对angularJS的新手问题,但没有看到类似的案例在教程搜索。将参数传递给几个AngularJS指令实例?
如何使用相同的指令定义将不同的参数传递给单独的div实例?这里我期望看到red green blue
,但我在HTML中看到blue blue blue
。我看到控制器在链接之前被调用。
http://jsfiddle.net/gradualstudent/Y2bBy/
<!DOCTYPE html>
<html >
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
<script>
var app = angular.module('myApp', []);
app.directive("element", function() {
return {
restrict: "A",
template: '<h1>{{type}}</h1>',
link: function (scope, element, attrs) {
scope.type = attrs.element;
console.log("Setting: "+scope.type);
},
controller: function ($scope) {
console.log("Checking: "+$scope.type);
}
};
})
</script>
</head>
<body ng-app="myApp">
<div element="red">1</div>
<div element="green">2</div>
<div element="blue">3</div>
</body>
</html>
范围:{类型:“@元素'}会更好 –
这个答案不适合我,我认为它混淆了我的指令(并没有加载),但是,然后再次,我使用ng 1.2 – Worthy7