Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have this code:

<li ng-repeat="objekt in driversList" class="mix {{objekt.Objekt.Type}}">
  <a href="objekttest.html"  data-ajax="false"
     ng-click="objekt.Objekt.Active='yes'">
    <img ng-src="../images/thumbs/{{objekt.Objekt.Thumb}}.jpg"
         style="margin-right:0;" >
    <span id="list">{{objekt.Objekt.Name}}{{objekt.Objekt.Active}}</span>
    <span id="listmala">{{objekt.Objekt.Type}}</span>
  </a>
</li>

objekt.Objekt.Active changes when I click the corresponding <li> tag.

However, on the other HTML link, I have:

<div ng-repeat="objekt in driversList">
  {{objekt.Objekt.Name}} {{objekt.Objekt.Active}}
</div>

This time, objekt.Objekt.Active keeps the default value (i.e. 'no').

Is it possible to change a scope variable permanently so that it is changed on some other HTML element?

Here's my controller code:

angular.module('aki', [
        'aki.controllers'
    ]);

    angular.module('aki.controllers', []).
    controller('akicontroller', function($scope,$rootScope) {
    //$scope.toggle = function(){
        //$scope.driversList.
    //}
    $rootScope.active='da';
    $scope.driversList = [
      {
          Objekt: {
              Name: 'Saint & Sinner',
              Type: 'nightclub',
              Thumb: 'Sinner',
              Active:'no'         
          }
      },
      {
          Objekt: {
              Name: 'Corner Cafe',
              Type: 'cafe',
              Thumb: 'corner caffe',
              Active:'no'
          }
      },...
...

EDIT: I'm making multipage application without Ajax

share|improve this question
    
Your ng-click should use a function in $scope to set the original object's value. ng-click="setActive($index)" otherwise you're only changing the value within the subscope created by ng-repeat. –  m.e.conroy Apr 28 at 18:40
    
so if I have a function, variable will be also changed on the other html? –  Axon Apr 28 at 18:54
add comment

1 Answer

up vote 0 down vote accepted

Not without using some form of backend or storage.

Looking at your code, you appear to have hard coded the driversList into the .js file. You're also navigating to a new page, it appears. AngularJS doesn't edit that JS file, so that's not going to persist.

You'll need to either have a backend service that stores the state (in memory or database) via http request, have AngularJS not actually change location (putting all of this on the same page, and show/hiding it as you interact), or use something like HTMLStorage to save/load the data.

You can also use something like AngularJS's Firebase backend to serve as persistence for the data - https://www.firebase.com/quickstart/angularjs.html has a quick start guide.

share|improve this answer
    
Thank you for the answer.I'll look it up :)) –  Axon Apr 28 at 18:48
    
@Axon if you just need to store values across route changes a simple AngularJS service will do (as services are singletons). –  Sebastian Apr 28 at 18:51
    
Will Services persist data across page loads? They might be singletons, and thus good for persisting across controllers, but if you are completely loading a new page, it should be reloading the service from scratch every time, wouldn't it? –  Casao Apr 28 at 18:57
    
That's right. It will not survive an enforced page reload (e.g. hitting the reload button). But in SPAs page reloads are very uncommon. When the user clicks on a link it does not result in a page reload but just a route change (which a service would survive). –  Sebastian Apr 28 at 19:01
    
but i use multi page app and i'm loading new page html with the same controller.Link is without AJAX –  Axon Apr 28 at 19:04
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.