return {
restrict: 'AE',
controller: 'myAwesomeCtrl',
templateUrl: '/myAwesome.html',
replace: true,
link: function (scope, element, attrs) {
attrs.$observe('attributeName', function (x) {
scope.setAttrName(x);
});
}
};
在 ng-repeat 和 ng-switch 中声明:
<div ng-repeat="f in fs">
<div ng-switch on="f.Name">
<myDirective attribute-name="{{f.AttributeName}}" ng-switch-when="thisIsTrue"></myDirective>
</div>
</div>
令人惊讶的是,如果我从指令的控制器访问 $attrs,它确实会正确插入 {{f.AttributeName}}。
$attrs.$observe('attributeName', function (x) {
$scope.setAttrName(x); // value of x is 'TheValue'
});
这不能是预期的行为,可以吗?v1.0.8
编辑:我刚刚确认它与 ng-switch 或 ng-repeat 无关......即使指令本身也不起作用。
这是塞子: http ://plnkr.co/edit/p46zuYbFAFCYH394zrUY?p=preview