简体   繁体   English

AngularJS:将服务变量绑定到控制器的$ scope上的值

[英]AngularJS: Bind a Service Variable to a Value on a Controller's $scope

I am creating a single-page AngularJS web app. 我正在创建一个单页AngularJS Web应用程序。 My app needs to be able to query a backend service to get some information about a user's state, and this state information needs to be available to the various pieces of the single-page app. 我的应用程序需要能够查询后端服务以获取有关用户状态的一些信息,并且此状态信息需要可用于单页应用程序的各个部分。 I created a service to manage this functionality, but I'm having trouble hooking things up in a way that seems reasonable to me. 我创建了一项服务来管理此功能,但是在以我认为合理的方式进行连接时遇到了麻烦。

Initially, I had things set up like this... 最初,我是这样设置的...

<service.js>
...
var url = 'www.my-backend.com';
this.val = [
    {
        name: 'undefined',
        isValid: false
    }
];
$http.get(url, {})
    .success (function (data) {
        this.val = data;
    })
    .error (function () {
        this.val =  [
            {
                name: 'error',
                isValid: false
            }
         ];
    });
...

And then in my controller... 然后在我的控制器中...

<controller.js>
...
$scope.val = service.val
...

This didn't work though ( val.name was 'undefined'), presumably because service.val was bound to the controller's $scope before the get request had a chance to terminate. 不过这没有用( val.name是“ undefined”),大概是因为在get请求有机​​会终止之前, service.val已绑定到控制器的$ scope。 However, that does seem at odds with what I read here . 但是,这似乎与我在此处阅读的内容有所不同。

The next thing I did was this... 我接下来要做的是...

<service.js>
...
var url = 'www.my-backend.com';
this.valPromise = $http.get(url, {});
...

And then in my controller... 然后在我的控制器中...

<controller.js>
...
$scope.val = [
    {
        name: 'undefined',
        isValid: false
    }
];
service.valPromise
    .success (function (data) {
        $scope.val = data;
    })
    .error (function () {
        $scope.val =  [
            {
                name: 'error',
                isValid: false
            }
         ];
    });
...

This worked, but I didn't like it. 这行得通,但我不喜欢它。 I feel like that logic belongs in the service. 我觉得这种逻辑属于服务范围。

So the next thing I did was work through the various suggestions that I found here , although none of them seemed to have the desired effect. 因此,我接下来要做的就是研究在这里找到的各种建议,尽管这些建议似乎都没有达到预期的效果。 I also saw this , but it seems like overkill and not really applicable to my problem. 我也看到了这一点 ,但似乎有点过分了,并且不适用于我的问题。

Ideally I'd really like to figure out how to get my first attempt working (tightly bind my service variable to my controller scope by reference), but if that's not something that can really be done within the Angular framework, I'm happy to use some kind of watch behavior. 理想情况下,我真的很想弄清楚如何使我的第一次尝试工作(通过引用将我的服务变量紧密地绑定到我的控制器作用域中),但是如果在Angular框架中不能真正做到这一点,我很乐意使用某种watch行为。 Can anyone spot what I'm doing wrong or why my service variable isn't getting properly hooked up to my controller? 谁能发现我做错了什么,或者为什么我的服务变量没有正确地连接到我的控制器上?

In your $http callbacks this is not the service. 在您的$http回调中, this不是服务。 You'll find plenty of answers about the meaning of this on SO. 你会发现很多有关的含义答案this对SO。 You need to refer to the service via a variable. 您需要通过变量引用服务。

The second problem is that this.val = data; 第二个问题是this.val = data; would assign a new value to the service, but doesn't change the data in the scope, which still points to the old array. 会为服务分配一个新值,但不会更改范围中的数据,该数据仍指向旧数组。 So you need to copy the new data to the existing array. 因此,您需要将新数据复制到现有阵列。

var service = this;
$http.get(url, {})
.success (function (data) {
    angular.copy(data, service.val);
})

I ran into this problem the other day. 前几天,我遇到了这个问题。 The reason it does not get set is because there is a race condition and because the data access is inside the service layer, it will lose every time. 之所以未设置它,是因为存在竞争条件,并且由于数据访问位于服务层内部,因此每次都会丢失。 It seems like there should be a easy way to do this. 似乎应该有一种简单的方法来执行此操作。 I ended up just having my initial controller make the data access calls and set the properties in the service for other controllers to get. 我最终只是让我的初始控制器进行数据访问调用,并在服务中设置属性以供其他控制器获取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM