angular.js在回调中触发ng-show

时间:2014-04-16 08:51:12

标签: angularjs asynchronous callback settimeout ng-show

我试图通过编辑setTimeout函数中的范围来触发ng-show。 setTimeout是这里的占位符,用于数据库查询回调。

的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)

}]);

这在setTimeout中如何实现? THX!

http://plnkr.co/edit/RPS2vZAlVfhliQKteSSK

更新:如上所述,setTimeout不是问题,它只用于生成可重现的stackoverflow问题。在我的项目中,我建立了一项服务:

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;

}]);

一样使用它
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 个答案:

答案 0 :(得分:2)

这是因为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;
});

答案 1 :(得分:2)

我注意到您在模板中绑定了{{show}}。应该删除大括号,因为它们位于AngularJS属性中。

答案 2 :(得分:1)

我的帖子的后期评论:mrida是正确的(thx),必须删除大括号......

<div ng-controller="c1">

    <div ng-show="show">

        test it

    </div>

</div>

也可以用ng-style解决它。

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)

...

答案 3 :(得分:-1)

使用$timeout代替setTimeout'show'代替'{{show}}'

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