我正在访问类名并在控制台中打印它们。这适用于编译,前置,后置但指令控制器出错。有人能解决吗?
//module declaration
var app = angular.module('myApp',[]);
//controller declaration
app.controller('myCtrl',function($scope){
$scope.name = "Peter";
});
//app declaration
app.directive('myStudent',function(){
return{
template: "Hi! Dear!! {{name}}<br/>",
compile:function(elem, attr){
console.log("compile");
return{
pre:function(scope,elem,attr){
console.log("pre" + attr.class);
},
post: function(scope,elem,attr){
console.log("post"+ attr.class);
}
}
},
controller: [function(scope, elem, attr){
console.log("controller" + attr.class);
}]
}
});
&#13;
<body ng-app="myApp" ng-controller="myCtrl">
<my-student class="one"></my-student>
<my-student class="two"></my-student>
<my-student class="three"></my-student>
<my-student class="four"></my-student>
<my-student class="five"></my-student>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.5/angular.min.js"></script>
</body>
&#13;
答案 0 :(得分:1)
这是因为您将获得控制器中的可注射服务。比如在控制器中
正如您所看到的,$ scope和$ attrs是使用angular内置的服务。
所以让你的控制器像
controller: function($scope, $attrs){
console.log("controller" + $attrs.class);
}
或
controller: ['$scope','$attrs',function($scope, $attrs){
console.log("controller" + $attrs.class);
}]
请按照plnkr
进行操作