Angularjs angular.js触发器在回调中显示

Angularjs angular.js触发器在回调中显示,angularjs,asynchronous,callback,settimeout,ng-show,Angularjs,Asynchronous,Callback,Settimeout,Ng Show,我试图通过在setTimeout函数中编辑作用域来触发ng显示。setTimeout是数据库查询回调的占位符 index.html: 如何在setTimeout内实现这一点?谢谢 更新:如上所述,setTimeout不是问题所在,它只用于生成可复制的stackoverflow问题。在我的项目中,我构建了一个服务: amProject1.service('asBasic', ['$http', '$q', function($http, $q){ var asBasic = {}

我试图通过在setTimeout函数中编辑作用域来触发ng显示。setTimeout是数据库查询回调的占位符

index.html:

如何在setTimeout内实现这一点?谢谢

更新:如上所述,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();

            // ...

        }
        // ...

这是因为setTimeout在
$scope
进程之外。注入并改用
$timeout

例如:

如果您确实想改为使用
$scope.apply
,我认为您的用法是错误的。在范围编辑之后,您不调用
$scope.apply()
,而是将它们放在apply函数中:

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

使用
$timeout
而不是
setTimeout
'show'
而不是
'{{show}'

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

测试一下

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

我的帖子的最新评论:mrida是对的(thx),必须删除花括号

<div ng-controller="c1">

    <div ng-show="show">

        test it

    </div>

</div>

Thx,您的$timeout解决方案可以工作,但这并不是问题所在。请看我的更新上面。用$scope.apply包装范围编辑对我不起作用。
amTestNgShow.controller('c1', ['$scope', '$timeout', function($scope, $timeout) {
    $timeout(function(){
        $scope.show = true;
    }, 1000);
}]);
$scope.apply(function(){
    $scope.show = true;
});
<div ng-show="show">
    test it
</div>
<div ng-controller="c1">

    <div ng-show="show">

        test it

    </div>

</div>
...

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

...