gpt4 book ai didi

javascript - Angular : Extract keyword from user input - and display it

转载 作者:行者123 更新时间:2023-12-02 14:22:19 25 4
gpt4 key购买 nike

我想允许用户以他/她喜欢的方式订购地址的格式。
为此,将有一个输入,用户可以在其中输入关键字和常规文本。
这个想法是检测何时输入关键字并从对象中提取该关键字,然后显示它。

这是对象:

$scope.address = {
"addressline1": "Street Name",
"addressline2": "City and County",
"addressline3": "12",
"country": "United Kingdom",
"postcode": "XE12 2CV"
}

这是标记:

<input ng-model='value' placeholder="address1, address2, address3, address4" type="text" />

该指令应该允许在文本字段中进行任何输入 - 但是 - 当输入关键字作为“值”时,脚本应该从地址对象中获取该关键字 - 并显示它。

我正在努力实现这一点。

下面是我的JS。

(function() {
'use strict';
angular.module('testApp', [])
.controller('testController', hotelController)

function hotelController($scope, $http) {
var vm = this;
$scope.address = {
"addressline1": "Street Name",
"addressline2": "City and County",
"addressline3": "12",
"country": "United Kingdom",
"postcode": "XE12 2CV"
}
var regexMobile = /addressline[0-9]*$/;
var match = regexMobile.exec($scope.address);
var result = '';
var startPoint = 1;

if (match !== null) {
for (var i = startPoint; i < match.length; i++) {
if (match[i] !== undefined) {
result = result + match[i];
}
}
$scope.value = result;
}
}
})()

JSfiddle

感谢任何帮助。

最佳答案

我从您的问题中了解到,您正在尝试根据用户在文本框中键入的内容来显示标签

例如:

用户类型:“addressline1”,因此应显示“街道名称”。

然后,如果我是正确的,下面的演示应该可以工作:

编辑:

根据作者的要求,此编辑版本展示了如何用逗号分隔项目,然后显示各自键中的值:

(function() {
angular
.module('testApp', [])
.controller('testController', hotelController);

hotelController.$inject = ['$scope'];

function hotelController($scope) {
$scope.address = {
"addressline1": "Street Name",
"addressline2": "City and County",
"addressline3": "12",
"country": "United Kingdom",
"postcode": "XE12 2CV"
}

$scope.change = function() {
$scope.label = "";
$scope.value.split(", ").forEach(function(value) {
for (var addressKey in $scope.address) {
if (addressKey == value) {
$scope.label += " " + $scope.address[addressKey];
}
}
})
}
}
})();
<!DOCTYPE html>
<html ng-app="testApp">

<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script>
</head>

<body ng-controller="testController">
<section class='container'>
<h5>Type in keyword</h5>
<p>Custom directive which filters out keywords from an object and outputs them as text</p>
<input type="text" ng-model="value" placeholder="address1, address2, address3, address4" ng-change="change()">
<hr>
<span ng-bind="label"></span>
</section>
</body>

</html>

关于javascript - Angular : Extract keyword from user input - and display it,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38555107/

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