javascript - AngularJS - Service talking to Controller -
angular js has pretty cool feature of two-way binding allows controller data automatically updated when view changes (or vice-versa). today can have controllers dependent on services. possible services depend on controller? e.g. if service wants fetch data controller on it's own will, possible?
also, possible extend scope of ng-model variable pass service (along controller) variable, keeps in sync automatically?
thanks.
when assign multiple variables same object , each reference same object.
following demonstration. may not preferred approach depending on use case
app.controller('mainctrl', function($scope, myservice) { $scope.mainmodel = myservice.myobject; }) app.controller('otherctrl', function($scope, myservice) { $scope.othermodel = myservice.myobject; }) app.factory('myservice', function() { return { myobject: { name: 'test' } } });
both of following inputs show same value
<div ng-controller="mainctrl"> name:<input ng-model="mainmodel.name"> </div> <div ng-controller="otherctrl"> name:<input ng-model="othermodel.name"> </div>
Comments
Post a Comment