나는 나를 도울 수 있기를 바랍니다.여러 입력에 동일한 지시. 있을 수있다? AngularJS
나는 지시문이 있습니다
.directive('checkField', ['$http', function($http) {
return {
require: 'ngModel',
restrict: 'A',
link: function (scope, ele, attrs, c) {
scope.$watch(function() {
if (attrs.ngModel === 'data.gender_value' && ele.val() !== '') {
//valid
} else {
//error
}
if (attrs.ngModel === 'data.cardholder_value' && ele.val() !== '') {
//valid
} else {
//error
}
});
},
template: ''
};
}])
을 그리고 난 내 HTML에서 여러 개의 입력이 있습니다
<input ng-model="data.cardholder_value" type="text" size="50" data-check-field />
<input ng-model="data.gender_value" type="text" ng-required="true" data-check-field />
문제는 그 시계 트리거는 더 이상, 첫 번째 입력 "을 참조하십시오"입니다를.
동일한 입력문을 여러 입력에 사용하려고하지만 작동하지 않습니다. 내가 경고를하면, 필드의 속성 이름을 확인하기 위해 항상 "data.cardholder_value"를 표시하고 다른 이름 필드는 표시하지 마십시오.
미리 감사드립니다.
편집 1 : 이것은 내 html로 호출하는
(NG는-포함) :
이<form method="post" id="formQuestion" name="formQuestion" ng-submit="sendForm()" novalidate ng-controller="questionForm">
{{data | json}}
<div class="slide-animate" ng-include="'/templates/default/partials/_fields/1_card_type.html'"></div>
<div class="slide-animate" ng-include="'/templates/default/partials/_fields/2_gender.html'"></div>
내 응용 프로그램 컨트롤러 :
angular.module('app.controllers')
.directive('checkField', ['$http', function($http) {
return {
require: 'ngModel',
restrict: 'A',
link: function (scope, ele, attrs, ctrl) {
scope.$watch(attrs.ngModel, function(val) {
console.log(attrs.ngModel, attrs.name, val)
});
},
template: ''
};
}])
.controller('questionForm', ['$scope', '$http', 'fieldApiService', function ($scope, $http, fieldApiService) {
...
안녕 PSL이 문제는 내 코드는, 다른 입력이 작동하지 않는 첫 번째 입력 작동한다는 것입니다. 그래서 당신이 전달하는 코드는 첫 번째 입력 값만 보여줍니다. 답변을 주셔서 감사합니다. – Sommer
@Sommer 내 데모를보고, 나는 거기에 두 입력을 가지고'콘솔'을보고, 거기에 그것을 로깅하고있다. – PSL
@ 서머 내 데모를 봤어? 두 가지 변경 사항이 모두 기록되는 것을 보시겠습니까? f12를 누르고 창문에있는 경우 콘솔을 엽니 다. – PSL