angglarJsでバックグラウンドデータを取得する例を解説します。
1.
<div ng-app="module" ng-controller="ctrl">
<table border="1" width="600">
<tr>
<td> </td>
<td> </td>
</tr>
<tr ng-repeat="v in data">
<td>{{v.name}}</td>
<td>{{v.url}}</td>
</tr>
</table>
</div>
<script>
var m = angular.module('module', []);
// http
m.controller('ctrl', ['$scope', '$http', function ($scope, $http) {
$http({
method:'get', //get
url:'1.php' //
}).then(function(response){
//
console.log(response);
$scope.data = response.data; //
},function(response){
//
console.log(response);
});
}]);
</script>
1.php
<?php
$data = [
[ 'name' => ' ', 'url' => 'www.baidu.com' ],
[ 'name' => ' ', 'url' => 'www.qq.com' ],
];
echo json_encode($data,JSON_UNESCAPED_UNICODE);
上記は最も簡単なhttpバックグラウンドデータの例です。もし一つのページに何度もバックグラウンドファイルをロードするなら、サービスを何回も要求しますか?明らかにこのようにしてページのロードを鈍らせることができます。キャッシュ操作によって、サーバーの圧力を減らして、できるだけ早くページデータを表示させることができます。では、具体的にはどうすればいいですか?簡単です。cache:trueをhttpに追加すれば解決できます。またページを更新する時は、一回の重複要求のデータだけが表示されます。
$http({
method:'get',
url:'1.php',
cache:true, //
}).then(function(response){
//
console.log(response);
$scope.data = response.data;
},function(response){
//
console.log(response);
});
もちろん、jqueryのajax要請のように、angglarjsも簡単に書くことができます。
m.controller('ctrl', ['$scope', '$http', function ($scope, $http) {
//post
//$http.post('1.php',{id:1})
$http.post('1.php').then(function(response){
//
console.log(response);
$scope.data = response.data;
});
}]);
m.controller('ctrl', ['$scope', '$http', function ($scope, $http) {
//get
//$http.get('1.php',{cache:true})
$http.get('1.php').then(function(response){
//
console.log(response);
$scope.data = response.data;
});
}]);
最後に、次の$httpサービスのバックグラウンドでPOSTデータを受信するいくつかの方法があります。
<div ng-app="module" ng-controller="ctrl"></div>
<script>
var m = angular.module('module', []);
m.controller('ctrl', ['$scope', '$http', function ($scope, $http) {
//
/* $http({
method:'post',
url:'1.php',
data:{id:1,name:' '}
}).then(function(response){
console.log(response.data);
})*/
//
$http({
method:'post',
url:'1.php',
data:$.param({id:1,name:' '}),
headers:{'Content-type':'application/x-www-form-urlencoded'}
}).then(function(response){
console.log(response.data);
})
}]);
</script>
<?php
#
//$content = file_get_contents('php://input');
//print_r(json_decode($content,true));
#
print_r($_POST);
以上のこのanglarJsのバックグラウンドデータを取得した例の解説は、小編集が皆さんに共有した内容の全部です。参考にしてもらいたいです。どうぞよろしくお願いします。