javascript - How to share scope between same controller twice -
i have controller appears in 2 locations on page. various reasons, has case.
so example simplified html structure this:
<aside ng-if="aside.on" ng-controller="mycontroller"></aside> <div>some other stuff</div> <main ng-controller="mycontroller"> <table ng-table="..."></table> </main>
now, when in aside
, expected able use $scope
, affect main element because use same controller. i've discovered though use same controller, have different scopes.
so i'm wondering, if because ng-if
, or if that's normal same controller in 2 locations. , also, how can share data between 2 controllers. in particular, using ngtable
, need add data table in main
, refresh when edit in aside
.
if trigger following doing in aside
, doesn't update main
table.
var newrow = { _id: "1234213434", name: "test", email: "test@test.com", accounttype: "admin", logins: 0, created: new date(), lastlogin: null, locked: false }; $scope.userlist.push(newrow); $scope.tableparams.reload();
"i expected able use $scope , affect main element because use same controller" wrong assumption
from angular's docs
when controller attached dom, angular instantiate new controller object
meaning can't share state among 2 controllers (like 2 instances of same class in java), can is:
- have parent scope wrapping both scopes , use scope share data
- use shared service
- broadcast messages between both controller (my least favored)
Comments
Post a Comment