Azad Shaikh
Azad Shaikh

Reputation: 61

Angularjs don't load controller if press back button

I am new to AngularJS.

I have 2 views, first one is for customer and other for city. The customer view has some basic fields to add information about customer, including button to add new city.

If I press the "add new city" button, then the view for adding new city is rendered. After this, when I go back on customer view, all previous entered information is lost.

How to keep this information?

Upvotes: 0

Views: 1387

Answers (4)

hisabimbola
hisabimbola

Reputation: 306

When you visit a view, angular instantiates the controller again, so if you want the data to be persistence in the app, you should make use of a service, and store the data there. So when the controller is being instantiated, the controller would load the data from the service.

Upvotes: 1

Frank van Wijk
Frank van Wijk

Reputation: 3252

You could add something like this after the form with the customer information. Evertyhing is done in the same template and same controller, so no route switching.

Note that the city model is updated immediately when you typ the field. If you do not want this, you could implement a saveCity method on the scope which write the city model into another model and call it via ng-click="saveCity().

<div ng-hide="citySaved">    
  <input type="text" ng-model="city" />
  <button type="button" ng-click="citySaved = true">Save city</button>
</div>

Upvotes: 0

Arun CH
Arun CH

Reputation: 11

The reason is you are pressing back button between pages which has a same parent controller. Add different controller for pages will solve your issue.

Upvotes: 1

Venu
Venu

Reputation: 184

Use ng-show to show the city view rather and hide it using ng-hide once city is added.

If you are looking for state maintenance, implement $route.

Upvotes: 1

Related Questions