AngularJS工厂双向数据绑定

用户名

我有一个带有一些绑定变量的角度控制器,还有一个产生数组的工厂(用于填充选择控件中的选项):

// Controller MyController
angular.module('users').controller('MyController', ['$scope', 'Authentication', 'MyFactory', 
    function($scope, Authentication, MyFactory) {
        $scope.user = Authentication.user;
        $scope.options = MyFactory.getOptions($scope.user.firstName, $scope.user.lastName);
        ...
    }
    ...
}

// Factory MyFactory
angular.module('users').factory('MyFactory', 
    function() {
        var _this = this;
        _this._data = {
            getOptions: function(firstName, lastName){
                return [
                    firstName + ' ' + lastName,
                    lastName  + ' ' + firstName
                    ...
                ];  
            }
        };
        return _this._data;
    }
);

第一次运行良好,但无法使控制器和工厂之间的数据保持同步。

预期的效果是,对参数的MyFactory.getOptions()更改会修改分配给的结果数组$scope.options

尼克·拉森(Nick Larsen)

它第一次起作用是因为您正在调用一个返回新数组的函数,然后您的视图仅引用该数组,而不再调用该函数。最简单的解决方案是$scope.$watch为您的$scope.user变量添加一个调用该MyFactory.getOptions函数的变量

// Controller MyController
angular.module('users').controller('MyController', ['$scope', 'Authentication', 'MyFactory', 
    function($scope, Authentication, MyFactory) {
        $scope.user = Authentication.user;
        $scope.options = MyFactory.getOptions($scope.user.firstName, $scope.user.lastName);
        $scope.$watch("user", function(newVal,oldVal,scope) {
            scope.options = MyFactory.getOptions(newVal.firstName, newVal.lastName);
        });
        ...
    }
    ...
}

反正就是这样。可能必须使用一些语法。


根据您的评论,尝试这样的事情:

// Controller MyController
angular.module('users').controller('MyController', ['$scope', 'Authentication', 'MyFactory', 
    function($scope, Authentication, MyFactory) {
        $scope.user = Authentication.user;
        $scope.options = MyFactory.getOptions($scope, "user");
        ...
    }
    ...
}

// Factory MyFactory
angular.module('users').factory('MyFactory', 
    function() {
        var _this = this;
        _this._data = {
            getOptions: function(scope, property){
                var updateableArray = [];
                function updateArray(user) {
                    //delete all elements of updateableArray
                    updateableArray.clear();
                    //add all the new elements of updateableArray from user argument
                    updateableArray.push(firstName + ' ' + lastName);
                    updateableArray.push(lastName  + ' ' + firstName);
                    ....
                }
                scope.$watch(property, function(newVal,oldVal,watchScope) {
                    updateArray(newVal);
                });
                updateArray(scope[property]);
                return updateableArray;  
            }
        };
        return _this._data;
    }
);

当然,有一种更好的组织方式,但希望它足以帮助您理解。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

AngularJS:如何使用带有双向数据绑定的工厂

来自分类Dev

Polymer-AngularJS双向数据绑定

来自分类Dev

AngularJS 双向数据绑定被 setTimeout 取消

来自分类Dev

工厂和视图之间的AngularJS数据绑定

来自分类Dev

无法绑定来自AngularJS工厂的数据

来自分类Dev

工厂和视图之间的AngularJS数据绑定

来自分类Dev

Angularjs双向绑定

来自分类Dev

Angularjs双向绑定

来自分类Dev

AngularJS-双向绑定

来自分类Dev

AngularJS双向绑定

来自分类Dev

AngularJS:双向数据绑定未在Controller中找到模型

来自分类Dev

AngularJS:打开新窗口并维护双向数据绑定

来自分类Dev

AngularJS <select>双向绑定<option>的数据来自AJAX调用

来自分类Dev

AngularJS双向数据绑定在指令中无法正常工作

来自分类Dev

使用AngularJS在HighCharts中单击事件和双向数据绑定

来自分类Dev

移动HTML内容并保留AngularJS双向数据绑定

来自分类Dev

AngularJS中双向数据绑定的简单实际示例

来自分类Dev

AngularJS中C3.js中的双向数据绑定

来自分类Dev

不适用控制器的双向数据绑定 - AngularJS

来自分类Dev

双向数据绑定问题

来自分类Dev

双向数据绑定问题

来自分类Dev

Android:双向数据绑定

来自分类Dev

将工厂数据绑定到angularjs控制器

来自分类Dev

与缓存工厂的angularjs数据绑定不起作用

来自分类Dev

将工厂数据绑定到angularjs控制器

来自分类Dev

双向绑定PHP和AngularJS

来自分类Dev

* ngFor中的双向数据绑定

来自分类Dev

EditText中的双向数据绑定

来自分类Dev

Angular js双向数据绑定