我正在尝试在 Angular Directive(指令)中访问和打印类名。但是,我没有得到想要的结果。相反,为所有值获取 undefined 。
//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"+ this.class) ;
},
post:function(scope,elem,attr){
console.log("post"+ this.class);
}
}
},
}
});
<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>
最佳答案
然后尝试使用 attr.class
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);
}
}
},
}
});
关于javascript - 在 Angular 指令中的前后链接中打印属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41136867/