gpt4 book ai didi

javascript - 将变量附加到字符串的正确方法是什么?

转载 作者:行者123 更新时间:2023-11-30 08:28:42 24 4
gpt4 key购买 nike

我有一个 Angular JS 页面。有一个搜索表单。当我单击该按钮时,我通过从 URL 加载 JSON 数据来执行搜索。

我的问题是,修改 URL 字符串以将值包含在表单中的正确方法是什么?

包括我失败的尝试,你可以看到我正在做一个 +searchCriteria+ 这显然是错误的......

感谢任何帮助!

<div ng-app="myApp" ng-controller="customersCtrl"> 

Search Field: <input type="text" ng-model="searchCriteria">
<button ng-click="myFunction()">Search</button>

[ ... ]

</div>

[ ... ]

<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$scope.myFunction = function() {
var url = 'http://localhost:8984/solr/carlistings1/select?indent=on&q=make:' + searchCriteria + '&wt=json';
alert("url="+url);
$http.get(url)
.then(function (response) {$scope.names = response.data.response.docs;});
}
});
</script>

最佳答案

你需要引用$scope:

var url = 'http://localhost:8984/solr/carlistings1/select?indent=on&q=make:' + $scope.searchCriteria + '&wt=json';

关于javascript - 将变量附加到字符串的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41113720/

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