1

I need to change the "controller as" name to ctrl in "_restaurantdiv.html" since I am using this partial file in different places under different controllers. Currently I am doing so by writing ng-controller along with ng-include. But the problem is, it reinitializes the controller which I do not want to happen. All I need is just a change in the "controller as" name.

<div ng-controller="resultsController as searchresults">    
  <div class="col-md-3 col-sm-4 pane" ng-repeat="venue in searchresults.venuedetails|orderBy: searchresults.filteroption">
    <div ng-include="'partials/_restaurantdiv.html'" ng-controller="resultsController as ctrl"></div>
  </div>
</div>
PCK
  • 1,254
  • 6
  • 20
  • 37
  • 1
    Why don't you remove the second controller as? What is the problem if you are using this template at many places? – Vivz Jul 17 '17 at 05:15
  • Prefer using routes or directives to `ng-include`, they are much more maintainable and allow for easy template re-use. – Aluan Haddad Jul 17 '17 at 05:16
  • Wherever I am using that template, I have to call the respective controller function from within the template. So I need to have 'ctrl' as my alias name in the template. – PCK Jul 17 '17 at 05:17
  • What is the respective controller function that you have to call? Why don't you declare this template in your main controller and reference it across your application? – Vivz Jul 17 '17 at 05:20
  • 2
    You really should use a `directive` here. Then you can pass in or inject the function you need. – Aluan Haddad Jul 17 '17 at 05:21
  • @AluanHaddad I already have one nested view. This will result in a second level of nesting which I think is unnecessary since the partial is just a small template with few lines. – PCK Jul 17 '17 at 05:22
  • @CKA but it has behavior and you are experiencing scoping issues. A directive seems logical. It also makes the code more readable. Looking at your code, I am having a hard time distinguishing between the _restaurant_, _venue_, and _search result_ concepts already. – Aluan Haddad Jul 17 '17 at 05:27
  • @AluanHaddad Okay, I will try the directive approach. Will let you know if I have any issues. Thank you! – PCK Jul 17 '17 at 05:31

0 Answers0