javascript - Angular sharing data between controllers using service -


i'm using service share data between controllers. in first controller call function this:

musclegroupservice.setstate(true); 

here code function in service :

  var state;    function setstate(x) {         state = x;     } 

x becomes true, reason state doesn't become true, stays undefined.

how can fix ?

edit

i believe i've managed find problem is, i'm not sure how solve it.

in index.html have line of code:

<div class="modal fade" ng-include src="'/content/templates/modals/admin/modalmusclegroup.html'"  id="addmodal"> </div> 

and page ng-include includes modalmusclegroup.html , controller modal window.

and modal widnow controller when smth. this:

musclegroupservice.isitembeingupdated()

it returns undefined don't watch variable state.

i believe can fixes using $rootscope , $rootscope.$broadcast , $rootscope.$on. there other way without using $rootscope ?

edit 2

here code:

part of ctrl1 (which should send data service):

$scope.updateitem = function (item) {     musclegroupservice.setstate(true);     musclegroupservice.setitemforupdate(item); }; 

relevat parts of service:

app.angularmodule.service('musclegroupservice', function(breeze, logger) {       breeze.config.initializeadapterinstance("modellibrary", "backingstore", true);      var servicename = "/breeze/musclegroup";      var manager = new breeze.entitymanager(servicename);      manager.enablesavequeuing(true);      var removeitem = breeze.core.arrayremoveitem;      var items = [];     var state;      var itembeingupdated;       return {          setstate: function(x) {             state = x;         },          isitembeingupdated : function() {             return state;         },          setitemforupdate : function(item) {              itembeingupdated = item;         },          getitembeingupdated : function() {             return itembeingupdated;         },          updateitem : function() {             if (itembeingupdated.entityaspect.entitystate.ismodified()) {                 savechanges();             }         }, 

here modal ctrl:

app.angularmodule.controller('adminmusclegroupmodalctrl', function ($scope, breeze, musclegroupservice) {      $scope.init = function () {         if (musclegroupservice.isitembeingupdated() == true) {             $scope.itembeingupdated = musclegroupservice.getitembeingupdated();               $scope.newname = itembeingupdated.name;             $scope.newdesc = itembeingupdated.description;         }     };      $scope.init(); }); 

here part of html ctrl1 :

    <tbody>                         <tr data-ng-repeat="item in items">                             <td>{{item.name}}                             </td>                             <td>{{item.description}}                             </td>                             <td> <button class="btn btn-primary" data-ng-click="updateitem(item)" data-toggle="modal" href="#addmodal"><i class="glyphicon glyphicon-pencil"></i> edit</button>      </tr>                     </tbody>   <div class="modal fade" ng-include src="'/content/templates/modals/admin/modalmusclegroup.html'"  id="addmodal"> </div> 

and modal html:

<div class="modal-body">             <form class="form-horizontal">                 <div class="form-group">                     <label for="inputname" class="col-lg-2 control-label">name</label>                     <div class="col-lg-10">                         <input type="text" class="form-control" data-ng-model="newname" id="inputname" placeholder="name">                     </div>                 </div>                 <div class="form-group">                     <label for="inputdesc" class="col-lg-2 control-label">description</label>                     <div class="col-lg-10">                         <input type="text" class="form-control" data-ng-model="newdesc" id="inputdesc" placeholder="description">                     </div>                 </div>             </form>         </div> 

try defining function below:

myapp.factory('myservice', function(){     var state;     return {         setstate:function(x){             state = x;         }     } }); 

Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -