angular.js - 關于指令link 中的創建變量問題
問題描述
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);}); }} })
如問題所示我現在,在link創建一個變量show,這個show用在模板表示是否hide可是 scope.show一直顯示true?不知道問題出現在哪里求賜教給位!謝謝但是console.log(scope.show)是同步改變的啊
問題解答
回答1:改:
elem.find('p').on('click',function(){ scope.show=!scope.show; scope.$apply();});
補充:
看文檔

文檔說了,如果是controller里的同步操作,或者是通過$http、$timeout、$interval的異步操作,scope.$apply()是自動執行的(Angular幫你做了)。但你這里顯然不符合條件,你使用了DOM API,所以需要手動顯示的調用一下scope.$apply()
文檔地址: scope
回答2:謝謝指教,看了文檔ng 自己很多自己的方法都會觸發apply,dom,累死settimeout的操作不會觸發apply
相關文章:
1. boot2docker無法啟動2. docker-compose中volumes的問題3. java - SSH框架中寫分頁時service層中不能注入分頁類4. docker容器呢SSH為什么連不通呢?5. dockerfile - 為什么docker容器啟動不了?6. docker安裝后出現Cannot connect to the Docker daemon.7. nignx - docker內nginx 80端口被占用8. node.js - antdesign怎么集合react-redux對input控件進行初始化賦值9. 關docker hub上有些鏡像的tag被標記““This image has vulnerabilities””10. 關于docker下的nginx壓力測試

網公網安備