javascript - watched scope is empty after route change -
i have controller i'm using 3 different views collects data. i'm watching 2 scopes in it. excerpt:
// predefine place before google maps api $scope.place = { formatted_address: '', url: '' }; // watch place in case of change (doesn't work otherwise) $scope.$watch('place', function () { $scope.poll.poll.location = $scope.place.formatted_address; $scope.poll.poll.urllocation = $scope.place.url; }); everything works fine on view 1, $scope.poll.poll.location , $scope.poll.poll.location gets filled data of google maps api.
but when click on button changes route (ui-sref="view2"), using same controller, scopes empty.
how can transfer watched scopes between multiple views same controller?
if have multiple states using same controller, both states it's own instance of controller. have pass data param second state.
like this:ui-sref="view2({poll: poll})"
don't forget add param state definition in $stateprovider.
$stateprovider.state("homepagestate", { url: "/home/", templateurl: "templates/homepage.html", controller: "homepagecontroller", params: { poll: null //initial } }); in controller can value calling $stateparams.poll
you make use of angular services. service instantiated once , reusable.
if have wait data unknown source, have take @ promise.when()
when(value, [successcallback], [errorcallback], [progresscallback]);
wraps object might value or (3rd party) then-able promise $q promise. useful when dealing object might or might not promise, or if promise comes source can't trusted.
Comments
Post a Comment