javascript - How can i use factory function for different AngularJS controllers? -
i have factory function want use in 3 different controllers, want use if factory code block inside if condition of controller , else factory code block want use in else condition of controller. how achieve task using factory appreciated.
so far tried code below...
factory.js
angular.module("app").factory('geotreefactory', function() { return { gettreecheck: function (geolocation) { if (geolocation.id === 5657){ $.each(geolocation.parent(),function(index,location) { if (location.id !== geolocation.id) { $.each(location._childrenoptions.data.items,function(index,child){ var disablechildid = 'disabled' + child.id; var model = $parse(disablechildid); model.assign($scope, true); }) var disableitemid = 'disabled' + location.id; var model = $parse(disableitemid); model.assign($scope, true); } }); } // if child checked disable parents try { var parent = geolocation.parent().parent(); var disableparentid = 'disabled' + parent.id; var parentmodel = $parse(disableparentid); parentmodel.assign($scope, true); } catch (err) { // ignore since happens when node selected has no children } //expand , collapse tree on parent check childrens can disabled. $scope.riskinprcsgeolocationtree.expand($scope.riskinprcsgeolocationtree.findbytext(geolocation.text)); $scope.riskinprcsgeolocationtree.collapse($scope.riskinprcsgeolocationtree.findbytext(geolocation.text)); //if parent item checked, disable children if(geolocation.items) { $.each(geolocation.items,function(index,location) { var disableitemid = 'disabled' + location.id; var model = $parse(disableitemid); model.assign($scope, true); }); } }, //else block function if ocnditions false gettreeuncheck: function(geolocation) { if (geolocation.id === 5657){ var getparent = geolocation.parent(); $.each(geolocation.parent(),function(index,location) { if (location.id !== geolocation.id) { $.each(location._childrenoptions.data.items,function(index,child){ var disablechildid = 'disabled' + child.id; var model = $parse(disablechildid); model.assign($scope, false); }) var disableitemid = 'disabled' + location.id; var model = $parse(disableitemid); model.assign($scope, false); } }); } // if child unchecked enable parent try { var parent = geolocation.parent().parent(); var checkedchildrens = []; (var i=0; i<selectedriskgeolocations.length; i++){ var checknodes = selectedriskgeolocations[i]; checkedchildrens.push(checknodes); } if (checkedchildrens.length === 0){ var disableparentid = 'disabled' + parent.id; var parentmodel = $parse(disableparentid); parentmodel.assign($scope, false); }; } catch (err) { // ignore since happens when node selected has no children } //if parent item unchecked, enable childrens if(geolocation.items){ $.each(geolocation.items,function(index,location){ var disableitemid = 'disabled' + location.id; var model = $parse(disableitemid); model.assign($scope, false); }); } } };
controller.js
var selectedctlgeolocations = []; var selectedctlgeolocationids = []; $scope.populatecontrolinprcsgeoloction = function(geolocation) { var pos = $.inarray(geolocation.text,selectedctlgeolocations); if (pos < 0) { selectedctlgeolocations.push(geolocation.text); selectedctlgeolocationids.push(geolocation.id); // if parent item checked, disable // children geotreefactory.gettreeifblock(); } else { selectedctlgeolocations.splice(pos, 1); selectedctlgeolocationids.splice($.inarray( geolocation.id, selectedctlgeolocationids), 1); // if parent item unchecked, enable // children geotreefactory.gettreeelseblock(); } };
alright, way off here, you're trying do?
app.factory('myservice', function(){ return { ifblock: function(){ // code run if condition true }, elseblock: function(){ // code run if condition false } } }); app.controller('main', function(myservice){ $scope.myevent = function(geolocation){ if(condition){ myservice.ifblock(geolocation); } else { myservice.elseblock(geolocation); } } });
you solve problem (and if understand problem correctly, preferred method):
app.service('myservice', function(){ return { go: function(geolocation){ if(condition){ // code run if condition true } else { // code run if condition false } } } }); app.controller('main', function(myservice){ $scope.myevent = function(geolocation){ myservice.go(goelocation); } });
Comments
Post a Comment