谁能告诉我是否可以在自定义 Angular 指令的控制器中要求和使用 ngModel。我试图远离链接功能。我看到大多数示例都使用链接功能,但我认为必须有某种方法可以在指令控制器中使用它?还是只能在链接功能中访问?我见过的一种方法,如下所示,给了我不确定的。不知道有没有别的办法??我正在尝试验证组件并在错误对象上设置无效类。
//directive
angular.module('myApp', [])
.directive('validator', function (){
return {
restrict: 'E',
require: {
ngModelCtrl: 'ngModel',
formCtrl: '?^form'
},
replace: true,
templateUrl: 'view.html',
scope: {},
controllerAs: 'ctrl',
bindToController: {
rows: '=',
onSelected: '&?' //passsed selected row outside component
typedText: '&?' //text typed into input passed outside so developer can create a custom filter, overriding the auto
textFiltered: '@?' //text return from the custom filter
ngRequired: "=?" //default false, when set to true the component needs to validate that something was selected on blur. The selection is not put into the input element all the time so it can't validate based on whether or not something is in the input element itself. I need to validate inside the controller where I can see if 'this.ngModel' (selectedRow - not passed through scope) is undefined or not.
},
controller: ["$scope", "$element", function ($scope, $element){
var ctrl = this;
ctrl.rowWasSelected;
//called when a user clicks the dropdown to select an item
ctrl.rowSelected = function (row){
ctrl.rowWasSelected = true;
ctrl.searchText = row.name; //place the name property of the dropdown data into ng-model in the input element
}
ctrl.$onInit = $onInit;
function $onInit (){
ctrl.ngModelCtrl.$validators.invalidInput = validate;
}
function validate (modelValue, viewValue) {
var inputField = ctrl.formCtrl.name;
var ddField = ctrl.formCtrl.listData;
inputField.$setValidity('invalidInput', ddField.$touched && ctrl.rowWasSelected);
return true;
}
}];
}
});
//template
<form name="validatorForm" novalidate>
<div class="form-group" ng-class="{ng-invalid:validatorForm.name.$error.invalid}">
<label for="name">Names</label>
<input type="name" class="form-control" name="name" placeholder="Your name" ng-change="typedText(text)" ng-model="ctrl.textFiltered" ng-blur="ctrl.validate()" ng-required="ctrl.ngRequired">
</div>
<ul ng-show="show list as toggled on and off" name="listData" required>
<li ng-repeat="row in ctrl.rows" ng-click="ctrl.rowSelected({selected: row}) filterBy:'ctrl.textFiltered' ng-class="{'active':row === ctrl.ngModel}">{{row}}<li>
</ul>
</form>
//html
<validator
rows="[{name:'tim', city:'town', state:'state', zip: 34343}]"
on-selected="ctrl.doSomethingWithSelectedRow(selected)"
typed-text="ctrl.manualFilter(text)"
text-filtered="ctrl.textReturnedFromManualFilter"
ng-required="true">
</validator>