angularjs服务没有我的情况下工作 [英] angularjs service doesn't work in my case

查看:125
本文介绍了angularjs服务没有我的情况下工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一个元素是我mainController

Below is an element is within my mainController

<img src="loader.gif" ng-hide="done_ajax"/>

和我的js看起来像这样

and my js look like this

//service
    app.service('request_service', function(){
        this.finished = false;
    });

//main ctonroller
    app.controller('mainController', ['$scope','request_service', function($scope,request_service){
            $scope.ajaxed = request_service.finished;
    }]);

// second controller
app.controller('secondController', ['$scope','request_service', function($scope,request_service){

    callAPI("getUser", {
                method: "GET"
            }).success(function (resp) {

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

    });

什么是错的是我的angularjs服务的使用情况如何?我想隐藏的mainController控制的装载机,通过传递当Ajax调用在我的第二个控制器完成的条件值。

What's wrong is my usage of angularjs service? I want to hide a loader that controlled by the mainController, by passing the conditional value when the ajax call is done in my second controller.

推荐答案

您请求您的服务的价值,你不看其是否有变化。因此,将最有可能(取决于顺序和时间的控制器初始化)是假的,并保持假的。

You request a value of your service and you do not watch it for changes. Therefore it will most likely (depending on the sequence and time your controllers are initialized) be false and remain false.

$scope.ajaxed = request_service.finished;

如果你想知道什么时候request_service.finished变化,它应该是的看着

If you want to know when the request_service.finished changes, it should be watched:

$scope.isFinished = aService.isFinished;

$scope.$watch(function() { return aService.isFinished; }, function(newV, oldV) {
    $scope.isFinished = newV;
});

这篇关于angularjs服务没有我的情况下工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆