0

I have a form and a view that shows data from that form. I want to separate form and view (which will be more that one).

Here is my code:

<div data-ng-controller="dataController" class="container">
    <div data-ng-view></div>
    <div ng-include="templates.simple" scope="data"></div>
</div>

And the included view shows initial data good, but does not react on any data change. How do I fix it?

data is and object with some fields. templates.simple is a scope variable with template url

Code example: http://plnkr.co/edit/ibrsBaq8osYuEODGiM6O

Ivan
  • 437
  • 1
  • 7
  • 17

1 Answers1

0

The reason why binding is not working is you are reinitalizing an createDataController which is again creating data object for that ng-view template. This could be solve by removing createDataController controller from route.

Code

$routeProvider
    .when('/', {
         templateUrl: 'form.html',
         //controller: 'createDataController'
    })

Plunkr Here

Update

Other way would be if you want to load your controller twice still it doen't make any sense though. You could do this by writing ng-init on outside div, Instead of declaring that variable from controller.

<div data-ng-controller="createDataController" ng-init="data = {name: 'texy'}">
    <div data-ng-view></div>
    <div data-ng-include="'template.html'"></div>
</div>

Updated Plunkr

Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
  • Nop... the issue is the scope of controller. The form is inside the ng-view and the template is out side, your exemple doesn't work at all – Fals Jul 09 '15 at 21:35
  • @Fals what is relation of form.. its all about prototypal inheritance. Take a look at updated answer – Pankaj Parkar Jul 09 '15 at 21:42
  • ng-view create a new scope and discard the parent one. If you don't define the property, as you did to fix the issue, the ng-view will exists isolated from the top controller. – Fals Jul 09 '15 at 21:45
  • @Fals I think you are new to angularJs.. see this API and tell me where does `ng-view` creates a isolated scope. Also please read this http://stackoverflow.com/questions/14049480/what-are-the-nuances-of-scope-prototypal-prototypical-inheritance-in-angularjs this will tell you how prototypal inheritance work in javascript – Pankaj Parkar Jul 09 '15 at 21:48
  • I will not get into this discussion! You solve the issue your way, and I solve using my way! And, at all we helped someone with something. And... well I Have only 2 years of angular, learning. – Fals Jul 09 '15 at 21:50
  • @Fals that not about solving the issue. its about you are sharing wrong knowledge without looking at the things.. Thats not really fair dude.. – Pankaj Parkar Jul 09 '15 at 21:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/82874/discussion-between-fals-and-pankaj-parkar). – Fals Jul 09 '15 at 21:52
  • @Fals why you downvoted again? As its correct answer..please read the docs before answering.. – Pankaj Parkar Jul 09 '15 at 21:56
  • Don't take stuff personal, The inheritance is the right awnsear! You are right! i did look the doc as you suggested! Thank you! https://github.com/angular/angular.js/wiki/Understanding-Scopes#ng-view – Fals Jul 09 '15 at 22:00
  • @Fals Kindly remove downvote. I took it personally because you did downvoted without knowing mine answer is correct..If you want point then I will upvote yours answer..but do care the OP and other person who are referring to the answer..Sharing wrong explaination is strictly prohibited here. – Pankaj Parkar Jul 09 '15 at 22:04
  • @PankajParkar is there a chance to avoid using ng-init, as the objects usully are much bigger than one property – Ivan Jul 10 '15 at 08:27
  • @Ivan first you tell me why you want to load controller twice? – Pankaj Parkar Jul 10 '15 at 10:01
  • @PankajParkar I am not trying to load controller twice, but I basically need two different views for the same controller – Ivan Jul 10 '15 at 10:22