angularjs - 在 Angular.JS 中找不到指令所需的 Controller

标签 angularjs controller directive

如标题所示,我在 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/

相关文章:

javascript - AngularJS 指令 : $timeout alternative

php - 锂框架 : how to obtain controller name in layout

angularjs - Angular 中的 this.model 指令内部

javascript - 网址中的 Angular 日期引用不正确

javascript - 无法弄清楚为什么页面加载在底部? Angular UI-Router 自动滚动问题

angularjs - Spring引导应用程序找不到index.html

javascript - 如何在发布后更改 Rails Controller 的 View ?

angularjs:来自服务的广播事件

vue.js - Vue-如何在vue组件中呈现HTML

delphi - 可以禁用特定的 Delphi 提示吗?