對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