我是Angularjs的初學者,所以如果我在理解上有誤,請糾正我。Angularjs自定義指令獲取viewValue爲undefined
首先,下面是我的代碼。我正在嘗試創建一個用於輸入驗證的自定義指令。
For example:
<input drtooltip-message type="text" ng-minlegth="3"> //if the length of the input value is less than 3 a tooltip with custom message should be shown
<input drtooltip-message type="text" ng-maxlegth="5">////if the length of the input value is greater than 5 a tooltip with custom message should be shown
的Html
<div ng-app="validationApp" ng-controller="mainController">
<input drtooltip-message type="text" name="name" class="form-control" ng-model="user.name" required tooltip="Tooltip on left" tooltip-placement="top" ng-minlength="5" ng-maxlength="8" >
</div>
的js
var validationApp = angular.module('validationApp', ['ui.bootstrap']);
validationApp.controller('mainController', function($scope) {
});
validationApp.directive('drtooltipMessage', function() {
return {
restrict: 'A',
template: '<input tooltip tooltip-placement="top" >',
replace: true,
require: 'ngModel',
link: function (scope, element, attrs, ctrl) {
ctrl.$parsers.push(function (viewValue) {
alert(viewValue);//always getting 'undefined'
}
}
};
});
我期待在輸入框中爲警戒值輸入的值,但我得到 '未定義'。
參考:What's the difference between ngModel.$modelValue and ngModel.$viewValue
請檢查我更新的問題。我試圖使用該指令作爲輸入字段中的屬性。 –