angular.js - 請(qǐng)教一個(gè)關(guān)于angularjs的小問題
問題描述
我是angularjs的一個(gè)新手,今天在項(xiàng)目中遇到這樣的一個(gè)問題我想在1S種以后給p隱藏掉,但是一直沒效果,求解,為什么一直沒反應(yīng)
<p ng-show='on'>111</p>app.controller(’myinfoCtrl’, [’$scope’],function($scope) { $scope.on=true; setTimeout(function(){console.log($scope.on)$scope.on=false;console.log($scope.on) },1000)}
問題解答
回答1:
<!DOCTYPE html><html ng-app='app'><head> <title></title> <script type='text/javascript' src='x.js'></script></head><body ng-controller='myinfoCtrl'><p ng-show='on'>111</p><script type='text/javascript'> var app = angular.module(’app’, []); app.controller(’myinfoCtrl’, [’$scope’,function($scope) {$scope.on=true;setTimeout(function(){ console.log($scope.on) $scope.on=false; $scope.$apply(); console.log($scope.on)},1000) }]);</script></body></html>
在回調(diào)函數(shù)里需要手動(dòng)調(diào)用$scope.$apply();
或者使用$timeouthttp://www.leifengta.com.cn/...
望采納,謝謝
回答2:
用$timeout:
app.controller(’myinfoCtrl’, [’$scope’,’$timeout’],function($scope,$timeout) { $scope.on=true; $timeout(function(){$scope.on=false; },1000)}
直接在setTimeout中改變$scope中綁定的變量不會(huì)觸發(fā)變量的臟數(shù)據(jù)檢查,變量變化不會(huì)被同步到界面,因此界面也不會(huì)做出更改。
回答3:
我沒用過setTimeout, 不過把setTimeout換成$timeout 可以實(shí)現(xiàn)你說的效果
回答4:
<p ng-show='on'>111</p>
app.controller(’myinfoCtrl’, [’$scope’],function($scope) {
$scope.on=true;setTimeout(function(){ $scope.$apply(function(){$scope.on=false; });},1000)
}用setTimeout不會(huì)觸發(fā)$digest循環(huán),建議用angular封裝好的$timeout吧
回答5:


@流星留步 為什么會(huì)報(bào)錯(cuò)?
相關(guān)文章:
1. boot2docker無法啟動(dòng)2. docker-compose中volumes的問題3. 關(guān)docker hub上有些鏡像的tag被標(biāo)記““This image has vulnerabilities””4. nignx - docker內(nèi)nginx 80端口被占用5. docker容器呢SSH為什么連不通呢?6. 關(guān)于docker下的nginx壓力測(cè)試7. docker安裝后出現(xiàn)Cannot connect to the Docker daemon.8. node.js - antdesign怎么集合react-redux對(duì)input控件進(jìn)行初始化賦值9. java - SSH框架中寫分頁時(shí)service層中不能注入分頁類10. dockerfile - 為什么docker容器啟動(dòng)不了?

網(wǎng)公網(wǎng)安備