attrs.rating
是您設置爲該屬性的表達式字符串。您可以在scope.$eval()
的範圍內對其進行評估。
angular.module('your-module')
.directive('starDirective', function() {
return {
restrict: 'A',
scope: false,
link: function(scope, element, attrs) {
var ratingExpression = attrs.rating;
var rating = scope.$eval(attrs.rating);
// Do something with rating.
}
}
});
scope: false
爲默認值,這意味着這個指令不創建與其父新的領域和股範圍。
但是,上述指令將不知道何時更新aRating
。如果您想更新指令aRating
更改,您可以使用隔離範圍和數據綁定。
angular.module('your-module')
.directive('starDirective', function() {
return {
restrict: 'A',
scope: {
rating: '=rating'
},
link: function(scope, element, attrs) {
// `rating` object is available as `scope.rating` and it will keep updated.
}
}
});
這將創建一個新的作用域的指令,並結合aRating
與新創建的範圍的rating
財產。新範圍是與父範圍分離的,這意味着它不會從父範圍原型繼承。所以它是創建可重用組件的好工具包。
=
符號雙向綁定rating
屬性與指令作用域的rating
屬性。這意味着scope.rating
將在aRating
更改時更新,將在scope.rating
更改時更新。
如果您對隔離範圍感興趣,請參閱Angular's documentation瞭解更多詳情。
你可以發送一個plunker示例的content.ratings和aRating嗎?評級與對象做什麼? – 2014-12-06 03:38:27