gpt4 book ai didi

javascript - 如何将 ng-model 从 Angular 1.6 组件传递到输入元素

转载 作者:行者123 更新时间:2023-11-28 04:47:36 25 4
gpt4 key购买 nike

我正在尝试创建一个 Angular 1.6 组件,它是输入标签的包装器。我正在创建提前输入或自动完成功能。我必须在 html 中使用 ng-model 来创建组件,这很好,但我希望在子输入元素中使用 ng-model。对于组件,您必须使用元素,并且不能像指令那样使用属性标签。我创建了一支代码笔来说明我正在尝试做的事情。

http://codepen.io/patrickliechty/pen/GWLZeX?editors=1011

我想使用 ngController 来更新输入元素中的值。在代码笔中,如果您键入输入,您将看到绑定(bind)的模型数据显示在输入元素下方。

这是代码:

angular.module('app', ['EntryField']);

angular.module('app').controller('DataController', ['$scope', function($scope) {
$scope.fieldDataArray = [{"label": "First Name", "content": ""}, {"label": "Last Name", "content": ""}];
console.log("$scope.fieldDataArray: ", $scope.fieldDataArray)
}]);

angular.module('EntryField', []).component('customAutoComplete', {
template: '<input type="text" name="input" ng-model="$ctrl.ngModelController.$modelValue" autocomplete="off"/><br>[{{$ctrl.ngModelController.$viewValue}}]',
require: {
ngModelController: "ngModel"
},
bindings: {},
controller: 'CustomAutocompleteController'
});

angular.module('EntryField').controller('CustomAutocompleteController', CustomAutoController);

CustomAutoController.$inject = ['$scope', '$element'];

function CustomAutoController($scope, $element) {
let ctrl = this;

ctrl.$onInit = function() {
ctrl.ngModelController.$parsers.unshift(function (inputValue) {
handleInputUpdate(inputValue);
});
}


function handleInputUpdate(inputValue) {
//Do autocomplete functionality
}

}
<html ng-app="app">
<head></head>
<body>
<div>Angular 1.x Auto Complete</div>

<div ng-controller="DataController">
<div ng-repeat="fieldData in fieldDataArray">
<div>{{fieldData.label}}</div>
<custom-auto-complete ng-model="fieldData.content"></custom-auto-complete>
</div>
</div>

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.min.js"></script>
</body>
</html>

最佳答案

我有一个非常有效的问题解决方案,但我想知道其他人是否有更好的解决方案。

var app = angular.module('app', ['EntryField']);

app.controller('DataController', ['$scope', function($scope) {
$scope.fieldDataArray = [{
"label": "First Name",
"content": ""
}, {
"label": "Last Name",
"content": ""
}];
console.log("$scope.fieldDataArray: ", $scope.fieldDataArray);
}]);

angular.module('EntryField', []);

angular.module('EntryField').component('customAutoComplete', {
template: '<input type="text" name="input" ng-model="$ctrl.fieldData.content" autocomplete="off"/><br>[{{$ctrl.fieldData.content}}]',
bindings: {
fieldData: "="
},
controller: function($element, $timeout) {
let ngModelController;
this.$postLink = function() {
if (!ngModelController) {
ngModelController = angular.element($element.find('input')).controller('ngModel');
ngModelController.$parsers.unshift(function(inputValue) {
handleInputUpdate(inputValue);
return inputValue;
});

function handleInputUpdate(inputValue) {
console.log("!!!!!!!!!!!!!!!!!Got some input: ", inputValue);
}
}
}
}
});
<!DOCTYPE html>
<html ng-app="app">

<head>
<meta charset="utf-8" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.js"></script>
</head>

<body>
<div ng-controller="DataController">
<div ng-repeat="fieldData in fieldDataArray">
<div>{{fieldData.label}}</div>
<custom-auto-complete field-data="fieldData"></custom-auto-complete>
</div>
</div>
</body>

</html>

关于javascript - 如何将 ng-model 从 Angular 1.6 组件传递到输入元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43221524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com