不能让我的服务价值初始分配后更新 [英] Can't get my service value to update after initial assignment

查看:97
本文介绍了不能让我的服务价值初始分配后更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是角度的服务来跟踪一些按钮的状态,以及是否应显示与否。我有问题,第一个任务后,我无法改变的值。

I am using an Angular service to keep track of the state of some buttons and whether they should be shown or not. I am having issues that after the first assignment, I cannot change the value.

app.service('EntityButtonStateService', function(){

    var state = {
        showDelete: false,
        showNew: false
    };

    this.getState = function(){
        return state;
    };
});

在我的控制器:

    var buttonState = EntityButtonStateService.getState();

table.on('select', function(e, dt, node, config) {
    var id = getSelectedRowId(dt);

    buttonState.showDelete = true; //this assignments works
    buttonState.showNew = true;
});
table.on('deselect', function(e, dt, node, config) {
    buttonState.showDelete = false; //this assignment does not work
    buttonState.showNew = true;

});

还有什么我做错了?

What could I be doing wrong?

推荐答案

更新:

看来,你最关心的是保持了按钮状态的数据模型同步你的UI状态。有几个方法我能想到的,这将帮助你做到这一点。首先,我要指出,你的服务,似乎只为两件事(showNew,的showDelete等)跟踪布尔值。这是一个非常简单的任务,一是最适合范围的变量,并且这个抽象到工厂将是优雅的抽象的缘故优雅的抽象展示。另外请注意,您的用户界面可能是非常紧密耦合到你的数据模型,无论如何,所以再次在这里抽象概念将是徒劳的。但是,这一切完全是显而易见的,这意味着也许你正在尝试做更多的东西......所以张贴的东西对你有用的缘故,这里是另一项建议(也注意到,小提琴仍然是有用的):

It appears that your biggest concern is keeping your data model for button state in sync with your UI state. There are a couple ways I can think of that will help you do that. First of all, I'll note that your service seems to only track boolean values for a couple of things (showNew, showDelete, etc.). It is a highly trivial task, one best suited for scoped variables, and to abstract this into a factory would be a showcase in elegant abstractions for the sake of elegant abstractions. Also note that your UI is likely very tightly-coupled to your data model anyway, so once again abstraction here would be futile. But all this is completely obvious, which means perhaps you are trying to do something more... So for the sake of posting something useful to you, here is another suggestion (also note, a fiddle would still be useful):

- >您的工厂实例设置为一个范围变量:

-> Set your factory instance to a scoped variable:

而不是 VAR buttonState = EntityButtonStateService.getState()的;

DO $ scope.buttonState = EntityButtonStateService.getState();

如果您还不熟悉,角的范围服务是提供自动2路结合 - 因此我先前的评论。所以,这可能会工作,让你的UI来保持与价值同步为 showNew 的showDelete 等。但是,有时更新 $范围可能会很麻烦(即如果你的更新在消化周期的晚期发生)。在麻烦的第一个迹象,我就缠上了我更新了 $超时,这将推动更新到下一消化,这通常是卓有成效的。

If you're not already familiar, Angular's scope service is what provides the automatic 2-way binding--hence my earlier comment. So, this will probably work, allowing your UI to stay in sync with the values for showNew and showDelete, etc. However, sometimes updating $scope can be troublesome (i.e. if your updates happen late in a digest cycle). At the first sign of trouble, I'll wrap my updates in a $timeout, which will push the updates to the next digest, and that usually does the trick.

祝你好运。

OLD:

尝试返回一个对象实例:

Try returning an object instance:

app.service('EntityButtonStateService', function(){

    var state = function(){
        this.showDelete = false,
        this.showNew = false
    };

    this.getStateService = function(){
        return new state();
    };

});

然后你应该可以直接在返回的服务实例的值进行操作。

And then you should be able to operate directly on the values of the returned service instance.

如果你想跟踪的状态服务中,再加入getter和setter方法​​,以个人价值。例如,一个通用的方法是这样的:

If you want to track the state inside the service, then add getter and setters to the individual values. For example, a generic approach would look like this:

app.service('EntityButtonStateService', function(){

    var state = {
        showDelete: false,
        showNew: false
    };

    this.get = function(item){
        return state[item];
    };

    this.set = function(item, value){
        state[item] = value;
    };
});

然后,你可以在其他地方使用该code:

Then you could use this code elsewhere:

// To set:
buttonState.set('showDelete', true);

// To get:
buttonState.get('showDelete'); // returns true

这篇关于不能让我的服务价值初始分配后更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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