angularjs与后台进行数据交互
1html 页面
这是页面自动加载部分
var app = angular.module(“myApp”, []);
app.controller(‘cartCtrl’, function ($scope, $http) {
$http({
method: ‘GET’,
url: “/index.php/Home/Cart/getCartInfo”
}).then(function successCallback(response) {
console.log(response.data);
if(response.data.code == -1){
layer.msg(response.data.msg)
}else{
$scope.cart = response.data.cart;
$scope.m = response.data.m;
$scope.code = response.data.code;
}
}, function errorCallback(response) {
});
2、如果页面出现点击事件
在自动加载下面加入点击时间
例如: 以删除为例
$scope.del = function () {
console.log( $scope.cart);
layer.confirm(‘是否删除商品?’, {
btn: [‘确认’,‘取消’] //按钮
}, function() {
var data = new Array();
var i = 0;
for (var c in $scope.cart) {
console.log©;
for(var m in KaTeX parse error: Expected '}', got 'EOF' at end of input: … if (scope.cart[c][m].checkbox == true) {
i=1;
}
}
}
console.log( KaTeX parse error: Expected '}', got 'EOF' at end of input: … var k=scope.cart
console.log( JSON.stringify(k));
$http({
method: ‘POST’,
data: {‘cart’:JSON.stringify(k)},
headers: {‘Content-Type’: ‘application/x-www-form-urlencoded’},
transformRequest: function (obj) {
var str = [];
for (var p in obj) {
str.push(encodeURIComponent§ + “=” + encodeURIComponent(obj[p]));
}
return str.join("&");
},
url: “/index.php/Home/Cart/delete”
}).then(function successCallback(response) {
if(response.data.code==-1){
layer.msg(response.data.msg,{time:1000})
}
layer.msg(“删除商品成功”, {time: 1000});
window.location.href = “/Home/Cart/cart”;//
// window.history.go(-1);
}, function errorCallback(response) {
errorHandle(response);
});
}
}, function(){
layer.msg(‘已取消’);
})
}
3、后台