AngularJS factory two way data binding -



AngularJS factory two way data binding -

i have angular controller binded variables, , mill produces array (used populate options in select control):

// 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); ... } ... } // mill myfactory angular.module('users').factory('myfactory', function() { var _this = this; _this._data = { getoptions: function(firstname, lastname){ homecoming [ firstname + ' ' + lastname, lastname + ' ' + firstname ... ]; } }; homecoming _this._data; } );

it works first time, not maintain info in sync between controller , factory.

the intended effect alter in argument of myfactory.getoptions() modifies resulting array assigned $scope.options.

it works first time because calling function returns new array, , view ever references array, , never calls function again. easiest solution add together $scope.$watch $scope.user variable recalls myfactory.getoptions function.

// 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); }); ... } ... }

something anyway. might have play around syntax bit.

per comments, seek this:

// controller mycontroller angular.module('users').controller('mycontroller', ['$scope', 'authentication', 'myfactory', function($scope, authentication, myfactory) { $scope.user = authentication.user; $scope.options = myfactory.getoptions($scope, "user"); ... } ... } // mill myfactory angular.module('users').factory('myfactory', function() { var _this = this; _this._data = { getoptions: function(scope, property){ var updateablearray = []; function updatearray(user) { //delete elements of updateablearray updateablearray.clear(); //add new elements of updateablearray user argument updateablearray.push(firstname + ' ' + lastname); updateablearray.push(lastname + ' ' + firstname); .... } scope.$watch(property, function(newval,oldval,watchscope) { updatearray(newval); }); updatearray(scope[property]); homecoming updateablearray; } }; homecoming _this._data; } );

there improve way organize it, it's plenty help idea.

angularjs

Comments

Popular posts from this blog

java Multi query from Mysql using netbeans -

c# - DotNetZip fails with "stream does not support seek operations" -

c++ - StartServiceCtrlDispatcher don't can access 1063 error -