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.

0

2 Answers 2

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.

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

1 Comment

You are absolutely correct. I found another similar question after asking mine, so I will close this.
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?

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.