如标题所示,我在 ng-view 中使用父自定义指令(定义 Controller )时收到此错误,该 View 将内容替换为包含子自定义指令(需要父 Controller )和 ng- 的模板重复。
我创建了a simple plunker来说明我的问题:
主模板:
<div class="ng-view">
<div data-my-parent></div>
</div>
路线:
$routeProvider.when( '/', {
template: '<span>View loaded.</span>',
controller: [ '$scope', function( $scope ) {
$scope.items = [ 'item1', 'item2' ];}]});
父指令:
.directive( 'myParent', function() {
return {
replace: true,
template: '<ul><li data-my-child data-ng-repeat = "item in items"></li></ul>',
controller: [ '$scope', '$element', '$attrs', function( $scope, $element, $attrs ){}]};})
子指令:
.directive( 'myChild', function() {
return {
require: '^myParent',
link: function( scope, element, attrs, controller ) {}};})
基本上,我认为它必须处理临时嵌入系统(对于 ng-repeat),但我不知道它是否是错误的编码或错误。消除此错误的最佳方法是什么?需要带有“?”的可选 Controller ?
最佳答案
ngView
的内容被路由的模板替换,所以你的 <div data-my-parent></div>
迷路了。
关于angularjs - 在 Angular.JS 中找不到指令所需的 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24597957/