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

bis*_*ton 6 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>
Run Code Online (Sandbox Code Playgroud)

但是当我点击第一个按钮渲染下一个模板时,我得到了错误

Controller 'ngSwitch', required by directive 'ngSwitchWhen', can't be found
Run Code Online (Sandbox Code Playgroud)

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

Vla*_*274 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>
Run Code Online (Sandbox Code Playgroud)

缺少元素

<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>
Run Code Online (Sandbox Code Playgroud)

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