AngularJS: data binding between expressions and sessionstorage -


i'm new angular. have single page application navbar maps html "sections". each section visualized monitoring state of variable angular directives ng-show.

after first load sections loaded , html in browser. can operations , save object in sessionstorage. angular expression referred not load new data! classical data binding between expression , session storage. how can this?

here there snippet of html:

    <div class="container" ng-show="panels.isselected(2)" ng-controller="datacontroller pdc">         {{pdc.mydata.property_foo}}              </div> 

and controller load data sessionstorage

.controller('datacontroller', ['$window',  function($window) {    pdc = this;    //mydata object   pdc.mydata = json.parse($window.sessionstorage.getitem("mydata"));  }]); 

angular evaluates value of mydata @ first loading of page, instead of triggering new evaluation of expression each times mydata changes...that's problem...

edit: simulate problem in plnkr http://plnkr.co/edit/vg1igopsjlbupozysy03?p=preview can see, when press update value displayed session storage not updated. can see new values refreshing page.

you can pass function $scope.$watch evaluate data not present on $scope. instance, sessionstorage, this, someservice or data source really.

so, asking should inject $scope controller, set $watch return value of function, , update this.mydata said value.

app.controller('datacontroller', ['$window',  '$scope', function($window, $scope) {    pdc = this;    $scope.$watch(function () {     return $window.sessionstorage.getitem('mydata');   }, function (value) {     pdc.mydata = value;   });    pdc.update = function ( value ) {      $window.sessionstorage.setitem("mydata", value);    };  }]); 

alternatively, reverse (makes little bit more sense in opinion):

$scope.$watch(function () {   return pdc.mydata; }, function (value) {   $window.sessionstorage.setitem('mydata', value); });  pdc.update = function (value) {   pdc.mydata = value; }; 

updated plunker


Comments