我有一个小的Angular JS页面。有一个搜索表单。当我单击按钮时,我通过从URL加载JSON数据来执行搜索。
我的问题是,修改网址字符串以包含表单中的值的正确方法是什么?
我的失败尝试被包括在内,你可以看到我正在做一个明显错误的+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>
答案 0 :(得分:3)
您需要参考$scope
:
var url = 'http://localhost:8984/solr/carlistings1/select?indent=on&q=make:' + $scope.searchCriteria + '&wt=json';
答案 1 :(得分:1)
var url = 'http://localhost:8984/solr/carlistings1/select?indent=on&q=make:' + encodeURIComponent(searchCriteria) + '&wt=json';
答案 2 :(得分:1)
var url = 'http://localhost:8984/solr/carlistings1/select?indent=on&q=make:' + $scope.searchCriteria + '&wt=json';