1

I have a sample application, and I am trying to display my model in one view, and edit it in another. Here is my controller:

var app = angular.module('plunker', ['ngRoute']);

app.controller('MainCtrl', function($scope, $location) {
  var self = this;
  this.name = 'World';
  this.someData = {name: 'test'};
  this.someCopyData = {};

  this.theEdit = function(path){
    self.someCopyData = angular.copy(self.someData);
    $location.path(path);
  };
});

app.config(['$routeProvider', function($routeProvider) {
    $routeProvider
        .when('/', {
           templateUrl: 'foo.html',
            controller: 'MainCtrl as main'
        })
        .when('/edit', {
          templateUrl: 'fooedit.html',
          controller: 'MainCtrl as main'
        });


}]);

The partial HTML is extremely simple:

Data: {{ main.someData.name }}
<br>
<br>
<a ng-click="main.theEdit('/edit')" >Resolve View</a>

and:

<input name="name" type="text" ng-model="main.someCopyData.name"/>

Plunker here.

How can I get the copy of my model to be displayed? This is clearly a scope issue, but I haven't figured out yet a proper explanation for the behaviour.

Phil
  • 879
  • 2
  • 10
  • 23

2 Answers2

1

It seems you only have 1 view have you tried to make 2 views for the same model? can you tell me what your trying to achieve exactly?

crisam
  • 53
  • 9
  • If you look in the Plunker, I would like to show the model in `foo.html`, and edit a copy of it in `fooedit.html`. – Phil Dec 11 '14 at 20:06
1

AFAIR, controller is created each time anew, that is you will get a new instance when you navigate to edit. You might want to read this guide, in particular to this part:

Do not use controllers to: ... Share code or state across controllers — Use angular services instead.

Mykola Gurov
  • 8,517
  • 4
  • 29
  • 27
  • You are absolutely correct. I found another similar question after asking mine, so I will close this. – Phil Dec 11 '14 at 21:51