Javascript 在pre&;中打印属性;角度指令中的后置链接
我正试图在angular指令中访问和打印类名。但是,我没有得到想要的结果。相反,对于所有值都未定义Javascript 在pre&;中打印属性;角度指令中的后置链接,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,我正试图在angular指令中访问和打印类名。但是,我没有得到想要的结果。相反,对于所有值都未定义 //模块声明 var-app=angular.module('myApp',[]); //控制器声明 应用程序控制器('myCtrl',函数($scope){ $scope.name=“Peter”; }); //应用程序声明 app.directive('myStudent',function(){ 返回{ 模板:“嗨!亲爱的!!{{name}}”, 编译:函数(元素、属性){ console
//模块声明
var-app=angular.module('myApp',[]);
//控制器声明
应用程序控制器('myCtrl',函数($scope){
$scope.name=“Peter”;
});
//应用程序声明
app.directive('myStudent',function(){
返回{
模板:“嗨!亲爱的!!{{name}}
”,
编译:函数(元素、属性){
console.log(“编译”);
返回{
前置:功能(范围、要素、属性){
console.log(“pre”+this.class);
},
职位:职能(范围、要素、属性){
console.log(“post”+this.class);
}
}
},
}
});代码>
使用link函数的orattr
参数。
请注意,elem.attr('class')
将为您提供类的实际列表,包括通过angular-likeng绑定添加的类<代码>属性['class']
将为您提供原始的“一”、“二”
//模块声明
var-app=angular.module('myApp',[]);
//控制器声明
应用程序控制器('myCtrl',函数($scope){
$scope.name=“Peter”;
});
//应用程序声明
app.directive('myStudent',function(){
返回{
模板:“嗨!亲爱的!!{{name}}
”,
编译:函数(元素、属性){
console.log(“编译”);
返回{
前置:功能(范围、要素、属性){
console.log(“pre”+元素属性('class'));
},
职位:职能(范围、要素、属性){
log(“post”+attr['class']);
}
}
},
}
});代码>
然后尝试使用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);
}
}
},
}
});
app.directive('myStudent',function(){
返回{
模板:“嗨!亲爱的!!{{name}}
”,
编译:函数(元素、属性){
console.log(“编译”);
返回{
前置:功能(范围、要素、属性){
控制台日志(“预”+属性类);
},
职位:职能(范围、要素、属性){
控制台日志(“post”+属性类);
}
}
},
}
});
mat我知道undefined的预期输出是什么?一、二、三、四、五为什么不使用attr来获取arributes而不是这个?好的。我认为“this”应该有效。我认为,您也可以直接从参数中使用attr对象。而不是使用elem.attr