0
votes

Please help me understand scopes in AngularJS.

  1. If I associate a controller within a directive (as opposed to within html), is it supposed to have any impact on the scope associated with the directive ?

  2. How can I use ng-repeat after scope isolation ?

For e.g. here is an example: http://plnkr.co/edit/0flo5mru61r9h3H8kiW5?p=preview

ex1. If I comment out (div ng-controller="Ctrl")[line 40, 43] and instead uncomment (// controller: 'Ctrl')[line 35] within the directive, why aren't the same scopes/hierarchy created (as viewed in Batarang).

ex2. How can I run ng-repeat for instructorList and profList (separately) without changing the current controller and only playing around with the scope ?

1

1 Answers

1
votes

I am not sure how to inspect plunkers in batarang, but.

  1. If you do this, you're instantiating the controller twice: once on each directive element. Each time you instantiate it, you're creating a new scope. As such, you have two separate sibling scopes. You can see just from looking at the html that the heirarchy won't be the same as if you had them both within the same element that has its own scope. In the latter case, changes made by child element 1 would affect the same scope used by child element two.

  2. It's not very clear what you mean here. ng-repeat should be done in html. You could put it in the template like this:

    template: '<label ng-repeat="person in teacherList">{{person.id}}<input ng-model="person.name"><br></label>'

See this