0
在試圖調試與角度的要求指令的問題,我有重複的功能自己:
.directive("myRequired", function(){
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, elm, attr, ctrl) {
if (!ctrl) return;
attr.myRequired = true; // force truthy in case we are on non input element
ctrl.$validators.myRequired = function(modelValue, viewValue) {
return !attr.myRequired || !ctrl.$isEmpty(viewValue);
};
attr.$observe('myRequired', function() {
ctrl.$validate();
});
}
};
})
我使用的md-select
輸入與該指令下列選項:
$scope.options = [
{ name: "option1", id: 1 },
{ name: "option2", id: 2 },
{ name: "option3", id: 3 }
];
當我的模型對我的選擇預先填充值,就像這樣:
$scope.myModel = {mySelect: 1};
然後myRequired
指令失敗,因爲ctrl.$isEmpty()
返回viewValue
(即,或者true
。
它爲什麼這樣做?
如何讓它返回一個布爾值?
這是奇怪的,因爲'$ isEmpty' [總是返回一個布爾](https://github.com/angular/angular.js/blob/c8d349870812284a82896087c05c2d19c8f5cdfd/src/ng/directive/ngModel.js#L363)。你可以發佈一個闖入者來重現問題嗎? –
如果您的選擇有預先填充的值,您期望什麼行爲?在我看來,驗證應該成功,因爲存在價值。驗證不成功嗎? –
正確,驗證失敗 – kat