angularjs access value of one controller from another -


<div ng-controller="actrl">  <input type="text" ng-model="person" />  <button type="button" ng-click="search()">xxx</button> </div>  <div ng-controller="bctrl">  <input type="text" ng-model="car" /> </div> 

in onclick event search() how gain access $scope.car, inside controller?

here's simple solution:

<div ng-init='model={ person: "", car: "" }'>   <div ng-controller="actrl">    <input type="text" ng-model="model.person" />    <button type="button" ng-click="search()">xxx</button>   </div>    <div ng-controller="bctrl">    <input type="text" ng-model="model.car" />   </div> </div> 

simply go 1 step higher on $scope , set data on parent scope. notice updated values bound inputs reference properties of new model object. doing this, reference object on parent scope rather scalars, reference object gets prototypically inherited child scopes. if doesn't make sense, tl;dr version is:

generally speaking, want make sure ng-model directives contain dot.

you define angular service share data between 2 controllers, that's possibly overkill need.


Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -