2016-05-18 106 views
0

我在我的簡單指令中使用了require: '^form'在指令模板中使用角度表格

然後我試圖在ng-show中使用這個表單,但它似乎不起作用。

注意:我不想將表單名稱作爲屬性傳遞。

任何人都可以看到我要去哪裏錯了嗎?我只希望消息在表單無效時顯示。

angular.module('xxx').directive('errorWall', errorWall); 

function errorWall() { 
    return { 
     restrict: 'E', 
     require: '^form', 
     scope: {}, 
     link: (scope, elm, attrs, frm) => { 
      scope.formCtrl = frm; 
     }, 
     template: '<div ng-show="formCtrl.$invalid">You have error messages.</div>' 
    }; 
} 
+0

當你在鏈接功能中放置一個斷點時,你會看到什麼? –

回答

1

確保您已將指令放置在表單中,並且至少有一個帶有ng-model指令的輸入。

<form> 
    <input type="text" ng-model="name" required /> 
    <error-wall></error-wall> 
</form> 

這裏有一個工作小提琴https://jsfiddle.net/3gv8nvL3/3/與一個表單所需的輸入。

+0

謝謝,我把它放在一個表格中,但現在看來我所使用的驗證框架正在干擾結果。感謝你的幫助 – Maurice