1

I am stuck in one scenario where i have to pass data from one controller to another controller.

My app works like this:

View 1 -> gets some users from json array and displays in a table.

When i click add user in View 1, i get redirected to view 2.

View 2 -> has 2 input fields and a button (to add a new user).

When i redirect to view 1, i want the new user to get appended but this is not working.

To make it easy, i have demoed this here - http://plnkr.co/edit/yz6mpplZGh4q5bPDO2bc?p=preview

Any help is much appreciated.

1

2 Answers 2

1

You are always overwriting the 'users' content on entry of the list controller.

Change your controller to

    ...
controller('UserController', function($scope,UserService){

if (!$scope.users)
{
  UserService.getUsers().then(function(data){
    $scope.users = data;
  })            
}
    ...

And bring back your 'correct'

 $scope.users.push($scope.user)

in your addUserController.

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

Comments

1

View2 directive needs to define that it requires View1. This will tell Angular to pass the controller for View1 as an argument to View2's controller.

You can then call a method addUser(..) on that controller.

https://docs.angularjs.org/guide/directive#creating-directives-that-communicate

1 Comment

Thanks for your point. This was new to me, i am exploring more on that the url you mentioned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.