如果在角度对象中找不到,如何在数据库中搜索项目.js?



当在对象中找不到搜索的数据时尝试发出ajax请求。

网页:-

Search programming languages: <input type="Text" ng-model="out.pl">
<div ng-controller="MyContRollEr">
<table border="2">
<tr>
<td>Programming Language:</td>
<td>Percentage:</td>
<td>Salary:</td>
<td>People:</td>
<td>Started Date:</td>
</tr>
<tr ng-repeat="data in info | filter:out">
<td>{{data.pl}}</td>
<td>{{data.percent}}</td>
<td>{{data.salary |currency:'Rs.'}}</td>
<td>{{data.people | number :2}}</td>
<td>{{data.date | date:"yyyy/MM/dd"}}</td>
</tr>
</table>

控制器:-

var app = angular.module('app',[]);
app.controller('MyContRollEr',function($scope) {
var info = [
{pl:'php',percent:'10%',salary:10000000,people:234524},
{pl:'Java',percent:'20%',salary:9822200,people:234443},
{pl:'python',percent:'10%',salary:8739300000,people:2345521)},
];
$scope.info = info;
});

我的职能:-

function sendRequest(){
$http({
method:'POST',
url:'index.php',
data:{search:'data'}
}).then(function(data) {
$scope.out = data;
})
}

如何结合我的控制器、功能和模型来做到这一点。

,这就是角度服务发挥作用的地方。应为控制器和服务创建一个新文件。不过,为了简单起见,您可以在控制器之后将以下代码添加到当前文件中。

app.service('myService',function($http) {
this.sendRequest = function() {
$http({
method:'POST',
url:'index.php',
data:{search:'data'}
}).then(function (response) {
console.log(response);
return response.data; // Most APIs have the "data" in the response object. But you can take this out if the console log doesn't show that key in the object.
})
}
)

完成后,您将在此处将服务注入控制器:

app.controller('MyContRollEr',function($scope, myService) { // Notice that the service is a parameter in controller now.

接下来,让我们通过点击服务来调用 POST 操作。在控制器块内,编写以下内容:

myService.sendRequest().then(function (response) {
console.log(response);
})

如果您没有使用 Gulp(或类似的东西(,那么您将需要将服务添加到您的索引.html(或任何文件是您的基本 html 文件(文件中,就像您使用控制器所做的那样(我假设(。

如果您有任何问题,请告诉我!

最新更新