- 当我在 input 中输入 char 时,它正在加载
type="search"
。 - Page 两次调用“ajax 调用”。请求在“单击”或“onPageload”时触发两次
索引.HTML
这是我的 html 文件,它有一个搜索框
<div>
<div class="input-group">
<input type="search" class="form-control search" ng-model="searchtag" tabindex="1">
<div class="input-group-btn">
<button class="btn btn-default" type="submit" ng-click="search()"></button>
</div>
<ng-view></ng-view>
</div>
这个搜索框没有附加,为什么会发生这种愚蠢的事情?
模板 url 正在加载ng-view
index.js 文件:
这里我定义了模板 url 的路由配置。
angular.module("myApp", ['ngRoute'])
.config(["$routeProvider", function($routeProvider) {
$routeProvider.
when("/", { templateUrl: "pic.html" }).
when("/Search", { templateUrl: "pic.html" }).
when("/VipList", { templateUrl: "cust.html" }).
otherwise({ redirectTo: "/", templateUrl: "pic.html" });
}]
)
在这里,我创建了一个服务,该服务调用将 ajax 调用发送到服务器并返回一个 json 对象。对每个请求调用两次 ajax 调用并返回两个“Json”数据对象。
.factory('Search', [function($http, $scope) {
return {
fetchPopular: function($scope, callback) {
$.ajax({
type: "POST",
url: 'Search',
data: {search : $scope.globeSearch},
dataType: 'json'
}).
success(function(data, status, headers, config) {
console.log(data);
if(data.data.length<=0){
alert("No data found");
}
callback(data.data);
})
.error(function(data, status, headers, config) {
});
}
};
}
])
.controller("fetch", function($scope, $interval, $location, Search) {
$scope.globeSearch="Sachin";
$scope.pics = [];
$scope.have = [];
$scope.orderBy = "-comments.count";
$scope.getMore = function(callback) {
Search.fetchPopular($scope, function(data) {
for(var i=0; i<data.length; i++) {
if (typeof $scope.have[data[i].id]==="undefined") {
$scope.pics.push(data[i]) ;
$scope.have[data[i].id] = "1";
}
}
$location.path("/Search");
});
};
$scope.getMore();
$scope.searchPopular = function(callback) {
if($scope.searchStr!=null && $scope.searchStr!=""){ $scope.globeSearch=$scope.searchStr; }
$scope.pics = [];
$scope.have = [];
$scope.orderBy = "-comments.count";
Search.fetchPopular($scope, function(data) {
for(var i=0; i<data.length; i++) {
if (typeof $scope.have[data[i].id]==="undefined") {
$scope.pics.push(data[i]) ;
$scope.have[data[i].id] = "1";
}
}
$location.path("/Search");
});
};