简体   繁体   English

角形验证-代码所需的设置(无效)

[英]angular form validation - set required by code (not working)

    if(currentAdmin.target =='new')
    {
        $('#btnDel').hide();
        //Not working !!!
        $('#inputPassword1').attr("ng-required","true");
        $('#inputPassword2').attr("ng-required","true");
    }
    else
    {
        $('#btnDel').show();
        $('#inputPassword1').attr("ng-required","true");
        $('#inputPassword2').attr("ng-required","true");
    }

Well, basically I want to place the ng-required based on the condition. 好吧,基本上我想根据条件放置ng-required。 I made a trial using the required (without ng) and it does not works as well. 我使用了必需的(没有ng)进行了试用,但效果不佳。

I inspected the element and injected the required and ng-required into the html and it's not works. 我检查了元素,并将必需和ng-required注入到html中,这是行不通的。 This will always be ignored after rendered. 渲染后始终将其忽略。 I need to do such thing like "Validator, refresh because it's different now" 我需要执行“验证程序,请刷新,因为现在有所不同”之类的操作

any clue ? 有什么线索吗?

Even though I myself hate the kind of answer I am about to give, I feel it is appropriate in this situation: This is not the way to write if using angular... 即使我自己讨厌我将要给出的答案,但在这种情况下,我仍然认为这是适当的:如果使用角形,这不是写的方法...

So what should you do? 那你该怎么办?

place the ng-required in the html code and set the value of the attribute to a bound variable that you set depending on your condition. 将ng-required放置在html代码中,并将属性值设置为根据条件设置的绑定变量。

Example: 例:

index.html 的index.html

<form ng-controller="formController">
  <input type="password" ng-required="isAdmin">
  <input type="password" ng-required="!isAdmin">
</form>

app.js app.js

angular.module('app',[]).controller('formController', function($scope){
  $scope.isAdmin=false;
  if(currentAdmin.target == 'new'){
    $scope.isAdmin=true;
  }
});

Not complete code, but hopefully you still understand what I mean. 不是完整的代码,但希望您仍然理解我的意思。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM