Hafsa Elif Elalmış Hafsa Elif Elalmış - 3 months ago 23
AngularJS Question

ANGULAR JS FACTROY FUNCTİON

I want the use set method of factory but both of return default how can i fix that problem?

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


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

return selectedArray;
},

setSelectedArray: function (array){

selectedArray=array;
}
}
});


Using ng-dual List from https://github.com/tushariscoolster/ng-duallist

I'm tkining it not working: DualListShareFactory.setSelectedArray(vm.rightValue);
I use other method for and push but ı am received same error .

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);


});

Answer

I am not this will work but try "this",

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

    this.selectedArray=[];

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

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