2014-09-30 34 views
-2

我無法弄清楚如何在以下情況下使用變量。 在HTML我已在在一個循環中執行以下操作:在此Angular指令中使用var來設置有效性

<span ng-show='myForm." + ids[i] + ".$error.missingInfo'>Wrong!</span>"; 

生成的HTML是正確的,這意味着IDS [I]使相應的HTML,是這樣的:

<span ng-show='myForm.foo.$error.missingInfo'>Wrong!</span>"; 

我有一個輸入元件,其具有使用自定義的驗證指令:

<input name="me-foo" id="foo-me" validateI /> 

在指令中,我想設置的「myForm.foo $ error.missingInfo」的有效性,所以我的指令:

app.directive('validateI', function(){ 
return{ 
    restrict: 'A', 
    require: 'ngModel', 
    link: function(scope, elem, attr, ctrl) { 

     ctrl.$parsers.unshift(function (viewValue) { 
      var id= attr.id; 
      var x= id.indexOf("-"); 
      //theId will be 'foo' 
      var theId= id.substring(x+1); 
      if(viewValue.length > 0) { 
       //this does not work 
       scope.myForm.theId.$setValidity("missingInfo", true); 
       //as a test, I hard-coded this and it worked: 
       scope.myForm.foo.$setValidity("missingInfo", true); 
      } 
       } 
      } 
      else{ 
       console.log("*** summary is empty"); 
      } 
     }); 
    } 
} 
}); 

有沒有在這種情況下使用變量的方式,否則我怎麼回事會得到何時縛住這一指令元素的名稱不是「富」「富」的錯誤信息?

回答

0

只需使用下面,使其工作:

CTRL $ setValidity( 「missingInfo」,真正的);

+0

不起作用,「CTRL」是聯繫在一起的元素的名稱,該指令是在 – bmw0128 2014-09-30 16:37:48

+0

什麼是myForm.foo?這是頁面上的實際字段嗎? – skewl84 2014-09-30 16:41:02

+0

是的,看看我的問題中的「span」元素,它是「ng-show」的一部分..訣竅是控制與綁定到驗證指令的元素名稱不同的錯誤消息 – bmw0128 2014-09-30 16:44:08

0

而是使用點符號的,用這個符號:

scope.myForm[id].$setValidity("missingInfo", false); 
相關問題