Reputation: 51
i tried to trigger an ng-show by editing the scope within a setTimeout function. setTimeout is a placeholder here for a database query callback.
index.html:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
<script src="script.js"></script>
<div ng-controller="c1">
<div ng-show="{{show}}" >
test it
</div>
</div>
script.js:
var amTestNgShow = angular.module('amTestNgShow',[]);
amTestNgShow.controller('c1', ['$scope', function($scope) {
// this works: $scope.show = true;
setTimeout(function(){
$scope.show = true; // works not
// does not help: $scope.$apply();
}, 1000)
}]);
how is this doable within setTimeout? thx!
http://plnkr.co/edit/RPS2vZAlVfhliQKteSSK
Update: As explained above, setTimeout is not the problem, it's only used to generate a reproducable stackoverflow question. In my project I builded a service:
amProject1.service('asBasic', ['$http', '$q', function($http, $q){
var asBasic = {};
asBasic.docName = '';
var doc = {};
asBasic.get = function(id){
var deferred = $q.defer();
$http({method:'GET', url: '/' + this.docName + '/' + id})
.success(function(data, status, headers, config){
if(data === undefined){
deferred.reject("The requested " + asBasic.docName + " was not found.")
}
doc = data;
deferred.resolve(doc);
})
.error(function(data, status, headers, config){
deferred.reject(status);
})
return deferred.promise;
}
return asBasic;
}]);
to use it like
amProject1.controller('acDoc1', ['$scope', '$routeParams', 'asBasic', function($scope, $routeParams, asBasic){
asBasic.docName = 'doc1';
// this works: $scope.show = true;
asBasic.get($routeParams._id)
.then(function(data){
$scope.doc1 = data;
$scope.show = true; // works not
// does not help: $scope.$apply();
// ...
}
// ...
Upvotes: 4
Views: 4733
Reputation: 51
A late review of my post: mrida was right (thx), the curly brackets must be removed ...
<div ng-controller="c1">
<div ng-show="show">
test it
</div>
</div>
It is also possible to solve it with ng-style.
html:
...
<div ng-controller="c1">
<div ng-style="{'visibility': show}">
test it
</div>
</div>
...
controller:
...
setTimeout(function(){
$scope.show = "visible"; // or $scope.show = "hidden"
$scope.$apply();
}, 1000)
...
Upvotes: 1
Reputation: 3962
Use $timeout
instead of setTimeout
and 'show'
instead of '{{show}}'
<div ng-show="show">
test it
</div>
Upvotes: -1
Reputation: 1157
I noticed that you are binding to {{show}} in your template. The curly brackets should be removed since they are inside an AngularJS attribute.
Upvotes: 2
Reputation: 33189
This is because setTimeout is outside of the $scope
processes. Inject and use $timeout
instead.
http://docs.angularjs.org/api/ng/service/$timeout
Eg:
amTestNgShow.controller('c1', ['$scope', '$timeout', function($scope, $timeout) {
$timeout(function(){
$scope.show = true;
}, 1000);
}]);
If you did want to instead use $scope.apply
, I think you simply had the usage wrong. You don't call $scope.apply()
after your scope edits, you instead put them inside the apply function:
$scope.apply(function(){
$scope.show = true;
});
Upvotes: 2