在下面的指令中,我們如何在通過指令中的屬性傳遞的其他元素上手動設置驗證。下面的代碼給出了錯誤otherField不存在,而它應該取這個變量的值,而不是直接在作用域字符串中的這個字段的名稱。從示波器角度動態訪問元素
app.directive('higherThan', [
function() {
var link = function($scope, $element, $attrs, ctrl) {
var otherField = $attrs.otherField;
$scope.otherField.$setValidity('lowerThan', true);
//Other details of directive not kept for simplicity
}
return {
require: 'ngModel',
link: link
};
}
]);
HTML
<form name="main_form">
<label>From</label>
<input name="from_date" type="text" class="form-control" ng-model="date_range[0]" lower-than="date_range[1]" ng-required="true" close-text="Close" />
<label>To</label>
<input name="to_date" type="text" class="form-control" ng-model="date_range[1]" higher-than="date_range[0]" ng-required="true" close-text="Close" other-field="from_date"/>
<span ng-show="main_form.to_date.$error.higherThan">From date should be lower than to date</span>
</form>
你能告訴我們你的HTML嗎? –