Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在pre&;中打印属性;角度指令中的后置链接_Javascript_Angularjs_Angularjs Directive - Fatal编程技术网

Javascript 在pre&;中打印属性;角度指令中的后置链接

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

我正试图在angular指令中访问和打印类名。但是,我没有得到想要的结果。相反,对于所有值都未定义

//模块声明
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函数的or
attr
参数。 请注意,
elem.attr('class')
将为您提供类的实际列表,包括通过angular-like
ng绑定添加的类<代码>属性['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