Angular.js trigger ng-show in callback

I tried calling ng-show by editing the scope in the setTimeout function. setTimeout here is a placeholder for the 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 can this be done in setTimeout? THX!

http://plnkr.co/edit/RPS2vZAlVfhliQKteSSK

Update. As explained above, setTimeout is not a problem, it is only used to create a reproducible stack question. In my project, I created 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;

}]);

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();

            // ...

        }
        // ...
+4
source share
4 answers

, setTimeout $scope. $timeout .

http://docs.angularjs.org/api/ng/service/$timeout

:

amTestNgShow.controller('c1', ['$scope', '$timeout', function($scope, $timeout) {
    $timeout(function(){
        $scope.show = true;
    }, 1000);
}]);

$scope.apply, , . $scope.apply() , apply:

$scope.apply(function(){
    $scope.show = true;
});
+2

, {{show}} . , AngularJS.

+2

: mrida was right (), ...

<div ng-controller="c1">

    <div ng-show="show">

        test it

    </div>

</div>

ng-.

HTML:

...

<div ng-controller="c1">

    <div ng-style="{'visibility': show}">

        test it

    </div>

</div>

...

:

...

setTimeout(function(){

    $scope.show = "visible"; // or $scope.show = "hidden"

    $scope.$apply();

}, 1000)

...
+1

$timeout setTimeout 'show' '{{show}}'

<div ng-show="show">
    test it
</div>
-1

Source: https://habr.com/ru/post/1536743/


All Articles