带有ng-include的Angular JS ng-switch?

时间:2015-01-25 02:42:56

标签: javascript angularjs angularjs-directive

我有3个不同的代码片段,我想根据选择菜单中的选择进行交换。

如果我将代码包含在内,它可以工作,但是当我尝试使用ng-includes这样的时候,我收到一个Angular错误并且应用程序失败了:

      <div ng-switch on="pFilter">
      <div ng-include="'includes/parcel_details_incoming.html'" ng-switch-when="Incoming Parcels"></div>
      <div ng-include="'includes/parcel_details_forward.html'" ng-switch-when="Exception Parcels"></div>
      <div ng-include="'includes/parcel_details_exception.html'" ng-switch-default></div>
      </div>

我在这里做错了什么? ng-switch不适用于ng-includes吗?

1 个答案:

答案 0 :(得分:10)

原因是指令ng-includeng-switch-x都使用了转换,并且您在同一元素上指定它们并且不允许。将nginclude移动到ng-switch元素的子节点。

   <div ng-switch on="pFilter">
      <div  ng-switch-when="Incoming Parcels"><div ng-include="'includes/parcel_details_incoming.html'"></div></div>
      <div  ng-switch-when="Exception Parcels"><div ng-include="'includes/parcel_details_forward.html'"></div></div>
      <div  ng-switch-default><div ng-include="'includes/parcel_details_exception.html'"></div></div>
   </div>

这曾经工作到角度1.x版本,但复合转换会导致 multidir 错误,从1.2.x版角度开始。请查看change log和此commit