gpt4 book ai didi

javascript - 如何使用 href 文本调用 ng-click 中的表单函数?

转载 作者:行者123 更新时间:2023-11-28 00:51:32 26 4
gpt4 key购买 nike

是否可以使用 href 文本值作为搜索变量 $scope.keywords 调用相同的函数 search()

function customersController($scope, $http) {
$scope.search = function() {
$scope.url = 'http://www.vanmaram.com/json_result.php?en=' + $scope.keywords; // The url of our search
$http.get($scope.url).
success(function(data, status) {
$scope.status = status;
$scope.data = data;
$scope.result = data; // Show result from server in <li> element
$scope.suggetionresult = null;
}).
error(function(data, status) {
$scope.data = data || "Request failed";
$scope.status = status;
});
};
$scope.suggestword = function(argument) {
$scope.url = 'http://www.vanmaram.com/ajax_json_suggestion.php?en=' + $scope.keywords; // The url of our search
$http.get($scope.url).
success(function(data, status) {
$scope.status = status;
$scope.data = data;
$scope.suggetionresult = data; // Show result from server in <li> element
$scope.result = null;
}).
error(function(data, status) {
$scope.data = data || "Request failed";
$scope.status = status;
});
};
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="" ng-csp ng-controller="customersController">
<form style="position:relative;" ng-submit="search()">
<input type="search" placeholder="Type english word" ng-model="keywords" ng-change="suggestword()">
<input type="submit" value="Search">
</form>
<ul ng-if='result.length'>
<li ng-repeat="word in result | limitTo:9">{{ word }}</li>
</ul>
<div id="suggestion" ng-if='suggetionresult.length > 1'>
Suggestions: <a ng-repeat="word in suggetionresult | limitTo:9" ng-click="search()">{{ word }}</a>
</div>
</div>

最佳答案

您可以向搜索功能添加可选参数。

$scope.search = function(searchUrl) {
//Check for searchUrl, otherwise use keywords.
var searchUrl = typeof(searchUrl) != 'undefined' ? searchUrl : $scope.keywords;

$scope.url = 'http://www.vanmaram.com/json_result.php?en=' + searchUrl;
$http.get($scope.url).
success(function(data, status) {
$scope.status = status;
$scope.data = data;
$scope.result = data; // Show result from server in <li> element
$scope.suggetionresult = null;
}).
error(function(data, status) {
$scope.data = data || "Request failed";
$scope.status = status;
});
};

然后ng-click="search(word)"

关于javascript - 如何使用 href 文本调用 ng-click 中的表单函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26718211/

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