我有一个自定义的包装器指令,用于angular-moment-picker。我使用cutrom model属性(dp-model)和一个内部ng-model (dpModelObject)。我想访问这个内部模型控制器来设置它的原始和有效性属性。
有可能吗?
(function() {
angular.module('app').directive('datePicker', datePicker);
datePicker.$inject = [];
function datePicker() {
return {
restrict: 'E',
scope: {
dpModel: '=',
dpRequired: '='
},
// replace: true,
templateUrl: template.html,
link: function($scope, $element, $attrs, ngModelCtrl)
{
if ($scope.dpModel) {
$scope.dpModelFormatted = moment($scope.dpModel, 'YYYY-MM-DD').format('YYYY. MM. DD.');
}
$scope.$watch('dpModelObject', function(date) {
if (date) {
$scope.dpModel = moment(date).format('YYYY-MM-DD');
}
}, true);
}
};
}
})();<div class="input-group datepicker">
<input type="text" class="form-control"
moment-picker="dpModelFormatted"
ng-model="dpModelObject"
ng-required="dpRequired"
>
<span class="input-group-addon">
<i class="glyphicon glyphicon-calendar"></i>
</span>
</div>
发布于 2017-12-13 18:20:53
您将需要在指令定义对象中设置require属性。
从文档中:
需要另一个指令,并将其控制器作为第四个参数注入链接函数。
return {
restrict: 'E',
require: 'ngModel', // here
scope: {
ngModel: '=', // and call it by the attribute name
dpRequired: '='
},
....然后,您的链接函数中的第四个参数将引用ngModelController,the official docs将更详细地介绍此内容
https://stackoverflow.com/questions/47789554
复制相似问题