我想用装饰有一个标准的输入控制ng-model
和ng-required
,然后添加自己的自定义属性的指令,它提供uib-typeahead
的功能来控制。
我使用此链接使我的指令部分起作用。
我的指令确实添加了预输入功能,并且效果很好,但是在选择项目之后,它并没有将模型绑定到控件上。
我有两个版本的指令。
版本1:是元素样式指令,并且我已经成功使用了一段时间,但是当我不想对输入元素进行更多控制时,尤其是当我想使用ng-required ='时,它就达不到要求了。 true”和其他ng-message指令。
版本2:是一个属性样式指令,我这样做了,因为我觉得最好将我想要的typeahead功能添加到可以选择使用的任何标准HTML中ng-required='true'
,ng-model
等等。
虽然该指令大部分有效,但它无法正确交互,因此ng-model
我不确定如何使它正常工作
angular.module(APP)
.directive('wkLocationSuggest', ['$compile', function ($compile) {
return {
restrict: 'A',
require: 'ngModel',
replace: false,
//terminal: true,
//priority: 0,
scope: {
wkApiModel: '=' // Provide access to the internal data that is returned via the API lookup
},
controller: 'LocationSuggestController',
link: function (scope, element, attrs, ngModelCtrl) {
if (!ngModelCtrl) {
return;
}
element.attr('typeahead', 'location as row.location for row in typeAhead($viewValue)');
element.attr('typeahead-wait-ms', '750');
element.attr('typeahead-on-select', 'onSelectInternal($item, $model, $label)');
element.attr('typeahead-min-length', '2');
element.attr('typeahead-focus-first', 'true');
element.removeAttr("wk-location-suggest"); //remove the location-suggest to avoid indefinite loop
element.removeAttr("data-wk-location-suggest"); //also remove the same attribute with data- prefix if it exists
// None of this is working
//// invoked when model changes from the outside
//ngModelCtrl.$render = function () {
// //scope.innerModel = ngModelCtrl.$modelValue;
//};
////// invoked when model changes from the inside
//scope.onChange = function (value) {
// ngModelCtrl.$setViewValue(scope.innerModel);
//};
scope.onSelectInternal = function ($item, $model, $label) {
// This fires, but it effects the ng-model on the first input,
// but not the input that this directive is attached too
ngModelCtrl.$setViewValue($item.location);
};
$compile(element)(scope);
}
};
}]);
这两个图像演示了部分问题,使用上面的PLUNKR可能会更好地进行自我测试
最初,我尝试通过结合使用的方法在input元素上实现,从而将验证器动态添加到您的wk-location-suggest-new
指令中;但不知道到底是什么原因阻止了该事件的触发。blur
ngModel
$setValidity
因此,我转向另一个指令,wk-location-suggest-old
并对其进行了一些微调以适合两种期望的行为。
在那里,我注意到您遗漏了几件事:
- 首先,为了使form元素与表单本身(在您的情况下)粘合在一起
wkProfileCompany
并使用ng-model
,该元素(在指令模板中)需要一个name。- 其次,
ng-required
(或required
)仅当表单作为属性添加到指令模板中的元素时才适用于该表单,而不是编译为包含该元素的模板的指令。
您可能会注意到,我已经将两个属性从外部范围传递到指令的内部范围,即:
name
输入元件的,isRequired
标志,用于指定是否需要输入。。
.directive('wkLocationSuggestOld', [function () {
return {
restrict: 'E',
require: '?ngModel',
scope: {
name: '@', // <==
isRequired: '=' // <==
},
template: '<input name="{{name}}" type="text" class="{{innerClass}}" ng-model="innerModel"'
+ ' ng-change="onChange()" uib-typeahead="location as row.location for row in typeAhead($viewValue)" '
+ ' typeahead-wait-ms="750" typeahead-on-select="onSelectInternal($item, $model, $label)" '
+ ' typeahead-min-length="2" typeahead-focus-first="true" '
+ ' ng-required="isRequired">', // <== added ng-required here
controller: 'LocationSuggestController',
link: function (scope, element, attrs, ngModel) {
if (!ngModel) {
return;
}
...
}])
最后,您可以在HTML中使用经过调整的指令,如下所示:
<wk-location-suggest-old class="form-control" type="text" name="location2" ng-model="location2" is-required="true"></wk-location-suggest-old>
ng-model
无法将wk-location-suggest-new
指令正确绑定到提供的值(即location3
)的可能原因之一是,您DOM
要用新的自定义DOM
元素替换整个元素,而该自定义元素是使用指令本身的隔离进行编译的。 scope
由于该指令wk-location-suggest-new
具有隔离范围,因此该范围完全不知道location3
,因为location3
(和所有其他位置值)是在MainCtrl
指令本身的范围内定义的,而不是在指令本身的范围内定义的;因此,您最终将把输入的值绑定到undefined
属性。
link: function (scope, element, attrs, ngModelCtrl) {
if (!ngModelCtrl) {
return;
}
...
$compile(element)(scope); // <== here
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句