Angular JS工厂功能

时间:2016-08-17 07:37:46

标签: javascript angularjs

我想使用工厂的set set方法但是返回默认值我该如何解决这个问题?

app.factory("DualListShareFactory", function(){

    var selectedArray=[];
    return{
        getSelectedArray: function () {

            return selectedArray;
        },

        setSelectedArray: function (array){

            selectedArray=array;
        }
    }
});

使用https://github.com/tushariscoolster/ng-duallist

中的ng-dual列表

我不能正常工作:DualListShareFactory.setSelectedArray(vm.rightValue); 我使用其他方法和推,但我收到同样的错误。

app.controller("duallist2", function($scope,DualListShareFactory){
    var vm=this;
    vm.property='duallist2';
    activate();

    function activate() {
        vm.leftValue = [];
        vm.rightValue = [];
        vm.addValue = [];
        vm.removeValue = [];

        function loadMoreLeft() {
            for (var i = 0; i < $scope.incomingItem.length; i++) {
                vm.leftValue.push({
                    'name': $scope.incomingItem[i]
                });

            }
        };

        function loadMoreRight() {
        }


        vm.options = {
            leftContainerScrollEnd: function () {
            },
            rightContainerScrollEnd: function () {
            },
            leftContainerSearch: function (text) {
                console.log(text)
                vm.leftValue = $filter('filter')(leftValue, {
                    'name': text
                })

            },
            rightContainerSearch: function (text) {

                vm.rightValue = $filter('filter')(rightValue, {
                    'name': text
                })
            },
            leftContainerLabel: 'Gelen Parçalar',
            rightContainerLabel: 'Seçilen Parçalar',
            onMoveRight: function () {
                console.log('right');
                console.log(vm.addValue);

            },
            onMoveLeft: function () {
                console.log('left');
                console.log(vm.removeValue);
            }

        };
        loadMoreLeft();

        var leftValue = angular.copy(vm.leftValue);

        var rightValue = angular.copy(vm.rightValue);

    }   console.log(vm.rightValue);
        DualListShareFactory.setSelectedArray(vm.rightValue);
    });

2 个答案:

答案 0 :(得分:0)

我不会这样做但是尝试&#34;这&#34;,

app.factory("DualListShareFactory", function(){

    this.selectedArray=[];

    return{
        getSelectedArray: function () {
            return this.selectedArray;
        },

        setSelectedArray: function (array){
            this.selectedArray = array;
        }
    }
});

答案 1 :(得分:0)

var app = angular.module("testapp", ["ng-duallist"]);
app.factory("DualListShareFactory", function(){
    var selectedArray = [];

    return{
        getSelectedArray: function () {
            return selectedArray
        },

        setSelectedArray: function (array){
            angular.copy(array, selectedArray);
        }
    }
});

使用此方法在结束函数之前添加其他代码块。

    onMoveRight: function () {
        DualListShareFactory.setSelectedArray(vm.rightValue);
    },
    onMoveLeft: function () {
        DualListShareFactory.setSelectedArray(vm.rightValue);
    }

};
loadMoreLeft();


var leftValue = angular.copy(vm.leftValue);

var rightValue = angular.copy(vm.rightValue);