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.

Community
  • 1
  • 1
Abhishek Saha
  • 2,564
  • 1
  • 19
  • 29

2 Answers2

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

Reactgular
  • 52,335
  • 19
  • 158
  • 208
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.

Marvin Smit
  • 4,088
  • 1
  • 22
  • 21