我应该能够将属性从 $scope 传递到这样的自定义指令中吗?
<div ng-repeat="ratable in ratables">
<div>How much do you like {{ratable.name}}?</div>
<!-- CONFUSED - First element does not work, second element does -->
<rating currentRatingValue="{{ratable.currentvalue}}" maxRatingValue="10"></rating>
</div>
硬连线的“10”可以很好地传递到指令中,但是当我尝试传递 {{ratable.currentvalue}} 时的字符串插值似乎永远不会发生。我在做一些明显错误的事情吗?
http://jsfiddle.net/ADukg/2168/
var myApp = angular.module('myApp',[])
.directive("rating", function () {
return {
restrict: "E",
scope: {},
template: "<div class='rating'>Current rating: {{currentratingvalue}} out of {{maxratingvalue}}</div>",
link: function (scope, element, attributes) {
console.log(attributes.currentratingvalue); // Does not work but seems like it should
console.log(attributes.maxratingvalue); // Does work
}
};
});
function MyCtrl($scope) {
$scope.name = 'Superhero';
$scope.ratables = [
{ name: "sledding", currentvalue: 3, maxvalue: 5 },
{ name: "water skiing", currentvalue: 7, maxvalue: 10 },
{ name: "whitewater rafting", currentvalue: null, maxvalue: 10 }
];
}
<div>
<div ng-controller="MyCtrl">
Hello, {{name}}!
<div ng-repeat="ratable in ratables">
<div>How much do you like {{ratable.name}}?</div>
<!-- CONFUSED - First element does not work, second element does -->
<rating currentRatingValue="{{ratable.currentvalue}}" maxRatingValue="10"></rating>
</div>
</div>
</div>