最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - In AngularJS 1.2, how do I specify the controller for an ng-include? - Stack Overflow

programmeradmin1浏览0评论

We are attempting to upgrade from AngularJS 1.0.7 to 1.2.1. In 1.0.7, we were able to set the controller for an ng-include alongside in the same element, like so

<div data-ng-include="'include1.html'" data-ng-controller="MyCtrl1"

MyCtrl1 would bee available to the code inside include1.html.

This breaks when moving to AngularJS 1.2.1 which I have illustrated in this plunkr. If you change the referenced version to 1.0.7 it works again.

I am interested in understanding what has changed/why this is. I tried searching but couldn't find anything or I am not using the right terms.

Additionally, what would be the correct way to specify a controller for my ng-includes?

We are attempting to upgrade from AngularJS 1.0.7 to 1.2.1. In 1.0.7, we were able to set the controller for an ng-include alongside in the same element, like so

<div data-ng-include="'include1.html'" data-ng-controller="MyCtrl1"

MyCtrl1 would bee available to the code inside include1.html.

This breaks when moving to AngularJS 1.2.1 which I have illustrated in this plunkr. If you change the referenced version to 1.0.7 it works again.

I am interested in understanding what has changed/why this is. I tried searching but couldn't find anything or I am not using the right terms.

Additionally, what would be the correct way to specify a controller for my ng-includes?

Share Improve this question asked Nov 20, 2013 at 13:23 MendhakMendhak 8,8446 gold badges55 silver badges75 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

Why not move the ng-controller from the element having ng-include to inside the template:

index.html:

<div data-ng-include="'include1.html'"></div>    
<div data-ng-include="'include2.html'"></div>

include1.html

<div data-ng-controller="MyCtrl1">
    <h1>{{Username}}</h1>
</div>

include2.html

<div data-ng-controller="MyCtrl2">
    <h1>{{Username}}</h1>
</div>

It seems ngController and ngInclude cannot be used in conjunction with each other since Angular version 1.2:

Issue 1, Issue 2, Issue 3 and SO post.

发布评论

评论列表(0)

  1. 暂无评论