簡體   English   中英

帶有ng-switch的角形

[英]Angular forms with ng-switch

當我得到de form的名字時,我正試圖從控制器的范圍中獲取一個表單對象。 它工作正常,但如果我使用ng-switch創建表單,表單永遠不會顯示在范圍內。

風景

<body ng-controller="MainCtrl">

    <div ng-switch on="type">
      <form name="theForm" ng-switch-when="1">
        <label>Form 1</label>
        <input type="text"/>
      </form>
      <form name="theForm" ng-switch-when="2">
        <label>Form 2</label>
        <input type="text"/>
        <input type="text"/>
      </form>

    </div>

    <button ng-click="showScope()">Show scope</button>
</body>

控制器

app.controller('MainCtrl', function($scope) {
  $scope.type = 1;

  $scope.showScope = function(){
    console.log($scope);
  };
});

如果我刪除了ng-switch,我可以從$ scope中看到屬性“theForm”作為表單obj。

知道如何做到這一點。 我不希望這兩個表單具有不同的名稱並使用ng-show。

以下是“不工作” http://plnkr.co/edit/CnfLb6?p=preview的示例

這是因為ngSwitch創建了新范圍。 (如果你看一下$$childHead是得到的范圍值console.log “d,你可以看到theForm里面,這是ngSwitch范圍)。

如果表單始終是相同的名稱,您只需將ngSwitch es放在表單中:

<form name="theForm">
  <div ng-switch on="type">
    <div ng-switch-when="1">
      <label>Form 1</label>
      <input type="text"/>
    </div>
    <div ng-switch-when="2">
      <label>Form 2</label>
      <input type="text"/>
    </div>
  </div>
</form>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM