Why angular syntax {{}} prevents change from attrs. $ Set in directive?

When applying angular syntax {{}}on an element, the directive attrs.$setwill not work.

Edit: My question is: can anyone explain why ?

If {{}}parsed, then linkwhy the item has not been changed to link?

If linkat first {{}}it should be deleted, both conditions will not look like this.

Here is the code handle

<div ng-app="ngApp" ng-controller="global">
  <a aaa href="http://{{::lan}}/4567">has syntax</a>
  <a aaa href="http://nosyntax/4567">no syntax</a>
</div>

angular.module('ngApp',[])
  .directive('aaa',function(){
  return {
    link:function(scope, ele, attr){
      attr.$set('href','http://fromdirective');
    }
  }
}).controller('global',function($scope){
  $scope.lan = 'en-gb';
})
+4
source share
3 answers

link, , {{}} ( ). , compile link ...

.directive('aaa',function(){
  return {
    compile: function(tElement, tAttributes, transcludeFn) {

      // -- just a jqLite object at this point
      tAttributes.$set('href','http://fromdirective');
    }
  }

, , . , $timeout , , link - , .

. SO ​​angularjs . ngHref - , .

+2

, ?

angular.module('ngApp',[])
  .directive('aaa',['$timeout',function($timeout){
  return {
    link:function(scope, ele, attr){
      $timeout(function() {
      attr.$set('href','http://fromdirective');
      },0);
    }
  }
}]).controller('global',function($scope){
  $scope.lan = 'en-gb';
})
+1

$timeout,

angular.module('ngApp',[])
  .directive('aaa',function($timeout){
  return {
    link:function(scope, ele, attr){
      $timeout(function(){
         attr.$set('href','http://fromdirective');
      });         
    }
  }
});
+1
source

All Articles