Angular 具有 {{}} 指令属性

Angular with {{}} directive attribute

有没有什么方法可以将 {{string}} 传递给从 $attrs(而不是 $scope)获取此属性的指令?

这是一些代码:

控制器:

...
scope.someId = "someId";
...

HTML:

<my-dir id="{{someId}}"></my-dir>

指令

app.directive('myDir', function() {
   return {
      controller: function($attrs){
         console.log($attrs.id) //output: {{someId}}
      }
   }
})

我想要的是输出 someId 而不是 {{someId}}.

在触发初始摘要周期之前,您将无法访问 $attrs 中的值。触发摘要循环后,您将能够从 $attrs.

访问它
app.directive('myDir', function() {
   return {
      controller: function($attrs){
         console.log($attrs.id) //output: {{someId}}
         $attrs.$observe('id', function (newVal, oldVal) {
             console.log(newVal, oldVal); // here you will be able to access the evaluated value. 
         });
      }
   }
})

这是一个很好且更专业的方法 it.In angularjs 关于范围字段类型有一个想法:

有3种。

1) 字符串@

2) 函数式&

3) 双向 =

这个风格better.Thislink可以帮到你

https://docs.angularjs.org/api/ng/service/$编译#指令定义对象