Note that there are some explanatory texts on larger screens.

plurals
  1. POAngularJS ng-repeat doesn't update when LocalStorage is changed (using store.js)
    primarykey
    data
    text
    <p>In my view I want to display a list of items (subject names) which are saved in a LocalStorage element. My code in the view looks like this:</p> <pre><code>&lt;div class="list"&gt; &lt;a class="item" href="#" ng-repeat="subject in subjects"&gt; {{subject.name}} &lt;/a&gt; &lt;/div&gt; </code></pre> <p>my controller looks like this:</p> <pre><code>.controller('SubjectCtrl', function ( $scope ) { $scope.subjects = store.get('subjects'); $scope.submit = function() { if (store.get('subjects') != null) { var existing = store.get('subjects') var subject = [ { name: $scope.form.name, weighting: $scope.form.weighting, grades: [] } ] subject.add(existing) store.set('subjects', subject) } else { var subject = [ { name: $scope.form.name, weighting: $scope.form.weighting, grades: [] } ] store.set('subjects', subject) } }; }) </code></pre> <p>The $scope.subjects variable gets the items from LocalStorage using Store.js (<a href="https://github.com/marcuswestin/store.js" rel="nofollow">https://github.com/marcuswestin/store.js</a>, a Plugin that simplifies LocalStorage access) and provides them to the view.</p> <p>The code underneath is triggered when the user submits a form to add a new subject. The form contains two inputs: name and weighting. When the form is submitted, the code checks if there are already any subjects inside the LocalStorage object 'subjects'. If yes, the new subject is added to the array of subjects and the LocalStorage is updated. If not, a LocalStorage object named 'subjects' is being created and the new subject is added.</p> <p>The code above works as intended, <strong>but my big problem is, that if a new subject is added to the array inside LocalStorage, Angular doesn't update the view</strong> and I have to reload the page manually to see the new subject appearing in the list.</p> <p>After some research, <strong>I have learned that the problem may be caused because the LocalStorage object is updated outside of AngularJS' scope</strong>. But I'm an Angular beginner and don't know what's the best way to notify Angular as soon as the object changes.</p> <p>I appreciate any help!</p> <p><strong>-- UPDATE --</strong></p> <p>I have switched from store.js (which doesn't work well together with angular) to ngStorage (<a href="https://github.com/gsklee/ngStorage" rel="nofollow">https://github.com/gsklee/ngStorage</a>). Maybe it's helping for somebody if I post my updated controller code using ngStorage:</p> <pre><code>.controller('SubjectCtrl', function ( $scope, $localStorage ) { $scope.$localStorage = $localStorage.$default({ subjects: [] }); $scope.subjects = $localStorage.subjects; $scope.submit = function() { $localStorage.subjects.push({ name: $scope.form.name, weighting: $scope.form.weighting, grades: [] }); }; }) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload