这个问题是以下的延续:
Call a function when ng-show is triggered?
我有一个嵌入在div中的javascript小部件。如果div的维度为0像素或被隐藏,则会抛出异常。
我有一个切换
<div ng-app="myApp" ng-controller="myCtrl" ng-init="isDisplayed = true">
<div ng-show="isDisplayed" id="kuku"></div>
<button ng-click="isDisplayed = !isDisplayed">Toggle</button>
</div>
var myApp = angular.module('myApp', [])
.controller('myCtrl', function($scope, $log) {
$scope.$watch('isDisplayed', function() {
if ($scope.isDisplayed) {
<create funky widget>
}
});
});
问题是在显示div之前调用了watch函数,我遇到了同样的问题。
我必须在显示div后调用该函数。我怎么能做到这一点?
答案 0 :(得分:1)
var app = angular.module("app", []);
app.controller("myCtrl", function ($scope, $timeout){
$timeout(function () {
$scope.$watch('isDisplayed', function() {
alert($scope.isDisplayed);
if ($scope.isDisplayed) {
alert("working")
}
});
});
});
<强> DEMO 强>