gpt4 book ai didi

angularjs - $scope.formName.fieldName.$setValidity 不起作用

转载 作者:行者123 更新时间:2023-12-03 08:25:36 24 4
gpt4 key购买 nike

当名字等于姓氏时,我想用 angular 设置无效,并使用样式将颜色更改为红色。

http://jsbin.com/japir/2

function RegoController($scope) {
$scope.app = {
firstName: "Saroj"
};

$scope.$watch("app.lastName", function(newVal, oldVal) {
if (!!$scope.app.lastName && !!newVal)
if (angular.lowercase($scope.app.firstName) === angular.lowercase(newVal)) {
debugger;
$scope.form.inputLastName.$setValidity("sameName", false);
}

});
}

<body ng-app>
<div class="container" ng-controller="RegoController">
<div class="col-lg-4">
<form name="form">
<div class="form-group">
<label for="inputFirstName">First Name</label>
<input id="inputFirstName" class="form-control" type="text" ng-model="app.firstName" placeholder="Enter your firstname" required ng-minlength="3" ng-maxlength="20" />
</div>
<div class="form-group">
<label for="inputLastName">Last Name</label>
<input id="inputLastName" class="form-control" type="text" ng-model="app.lastName" placeholder="Enter your last name" required ng-minlength="3" ng-maxlength="20" />
</div>
<div class="form-group">
<label for="inputEmail">Email</label>
<input id="inputEmail" class="form-control" type="email" ng-model="app.email" placeholder="Enter your email" required />
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Save" />
</div>
</form>
{{app}}
</div>
</div>
</body>

最佳答案

问题是您试图选择一个没有名称的表单输入;从而使其无法找到您试图无效的字段。这是一个工作示例:

JSBIN:http://jsbin.com/yozanado/1/

输入字段名称:

<input id="inputLastName" name="lastName" class="form-control" type="text" ng-model="app.lastName" placeholder="Enter your last name" required ng-minlength="3" ng-maxlength="20" />

Javascript:
$scope.form.lastName.$setValidity("sameName", false);

关于angularjs - $scope.formName.fieldName.$setValidity 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25216125/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com