2014-09-30 32 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); 
相关问题