页面上指令的属性为控制器(controller)$scope的值 如果在指令中要获取这个值,如果在link中用attr去获取,可能为underfind 是因为页面数据还没加载完成 最好的办法是在link中 用scope.$watch 去监听这个值。如下
function bookDetalis(){
return {
restrict: 'AEC',
replace: true,
scope:{
description:'@',
},
template:'<div>{{description}}</div>' ,
link: function(scope,elem,attrs) {
scope.$watch('description',function(){
if(scope.description){
console.log(elem[0].offsetHeight);
}
});
}
}
}
网友评论