无法找到指令'ngSwitchWhen'所需的AngularJS控制器'ngSwitch'

时间:2015-02-17 16:28:08

标签: javascript angularjs angularjs-directive

我有一个主要模板来做一些向导,比如单步执行一些模板:

<div ng-controller="StepController">
 <div ng-switch="step">
  <div ng-switch-when="1">
           <div ng-controller="ImportController">
              <div ng-include src="'static/javascripts/import/upload.html'">
           </div>
     <button type="button" class="btn btn-success btn-s"  ng-click="setStep(2)"> Next Step</button>
  </div>
  <div ng-switch-when="2">
     <div ng-include src="'static/javascripts/authentication/blank.html'">
      <button type="button" class="btn btn-success btn-s"  ng-click="setStep(3)"> Next Step</button>
  </div>
  <div ng-switch-when="3">
     <div ng-include src="'static/javascripts/authentication/blank.html'">
     <button ng-click="setStep(3)"></button>
  </div>
</div>

然而,当我点击第一个按钮渲染下一个模板时,我收到错误

Controller 'ngSwitch', required by directive 'ngSwitchWhen', can't be found

当我在点击按钮后查看源代码时,ng-switch div标签就在那里,所以我不确定它为什么不起作用。

1 个答案:

答案 0 :(得分:9)

我很确定你错过了一些关闭元素。我按照书面重新格式化了代码。

<div ng-controller="StepController">
    <div ng-switch="step">
        <div ng-switch-when="1">
            <div ng-controller="ImportController">
                <div ng-include src="'static/javascripts/import/upload.html'">
                </div>
                <button type="button" class="btn btn-success btn-s"  ng-click="setStep(2)"> Next Step</button>
            </div>
            <div ng-switch-when="2">
                <div ng-include src="'static/javascripts/authentication/blank.html'">
                    <button type="button" class="btn btn-success btn-s"  ng-click="setStep(3)"> Next Step</button>
                </div>
                <div ng-switch-when="3">
                    <div ng-include src="'static/javascripts/authentication/blank.html'">
                        <button ng-click="setStep(3)"></button>
                    </div>
                </div>

缺少元素

<div ng-controller="StepController">
    <div ng-switch="step">
        <div ng-switch-when="1">
            <div ng-controller="ImportController">
                <div ng-include src="'static/javascripts/import/upload.html'">
                </div>
                <button type="button" class="btn btn-success btn-s"  ng-click="setStep(2)"> Next Step</button>
            </div>
        </div>
        <div ng-switch-when="2">
            <div ng-include src="'static/javascripts/authentication/blank.html'">
                <button type="button" class="btn btn-success btn-s"  ng-click="setStep(3)"> Next Step</button>
            </div>
        </div>
        <div ng-switch-when="3">
            <div ng-include src="'static/javascripts/authentication/blank.html'">
                <button ng-click="setStep(3)"></button>
            </div>
         </div>
     </div>
</div>

问题在于,在错误的代码中,第二个和第三个ng-switch-when元素不在ng-switch元素的正下方。 ng-switch-when查看它的ng-switch语句的父元素,这是缺失的。