0

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?

1
  • Make use of service variables and not $scope variables if you want your ng-models to retain their values even after navigation. Commented May 7, 2015 at 7:05

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Abimbola Idowu:- u r right.. i understand thisconcept when going through problem... still i am not able to find proper solution.. i think i have to work on something like $route events
I do not really understand what you saying and why you said you want to use $route events, can you reproduce the problem on plnkr and would help you look through it
:- you are right buddy..... now i got the solution... you are right i keep my data inside service so it will work for me... now my data is persist... thanx buddy
1

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.

Comments

1

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.

1 Comment

yes u r right using different controller solved my problem but my requirement is to use 1 controller between multiple views
0

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> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.