angular.module("myDirective",[])
.directive("tabOne",function (){
return{
restrict:"E",
replace:true,
scope:{
data:"=myData",
},
transclude:true,
template:' <p ng-hide="show">'+
'<p ng-repeat="x in data">'+
'{{x}}'+
'</p>'+
'</p>',
link:function(scope,elem,attr){
scope.show=true;
elem.find("p").on("click",function(){
scope.show=!scope.show;
console.log(scope.show);
});
}
}
})
如問(wèn)題所示我現(xiàn)在,在link建立一個(gè)變數(shù)show,這個(gè)show用在模板表示是否hide可是 scope.show一直顯示true?
不知道問(wèn)題出現(xiàn)在哪裡求賜教給位!謝謝
但是console.log(scope.show)是同步改變的啊
改:
elem.find("p").on("click",function(){
scope.show=!scope.show;
scope.$apply();
});
補(bǔ)充:
看文件
文檔說(shuō)了,如果是
controller
里的同步操作,或者是通過(guò)$http
、$timeout
、$interval
的異步操作,scope.$apply()
是自動(dòng)執(zhí)行的(Angular
幫你做了)。但你這里顯然不符合條件,你使用了DOM API
,所以需要手動(dòng)顯示的調(diào)用一下scope.$apply()
文檔地址: scope
謝謝指教,看了文檔ng 自己很多自己的方法都會(huì)觸發(fā)apply,dom,累死settimeout的操作不會(huì)觸發(fā)apply