当我的页面加载时,我想从数据库加载数据。我通过angular的http模块获取数据,如下所示:
var getdata = function(){
var params = {
userId : 123,
};
$http.post("url", params)
.success(function(data,status,headers,config){
alert("success");
}).error(function(){
alert("Failed to connect with server");
});
};
在我的控制器中,我创建了一个名为onLoad的函数,并将其命名为:
var onLoad = function(){
getData();
console.log("load page");
};
onLoad();
但是,当我导航到该页面时,我收到以下错误 错误:[$ rootScope:inprog] http://errors.angularjs.org/1.5.0/ $ rootScope / inprog?p0 =%24digest
可能出现什么问题?谢谢!
答案 0 :(得分:0)
这是获取数据的方法,还有一件事我不知道你在控制器中添加了什么依赖,以防你没有这样做,这是正确的方法
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
$http({
method : "POST",
url : Your Url
}).then(function mySucces(response) {
$scope.myWelcome = response.data;
}, function myError(response) {
$scope.myWelcome = response.statusText;
});
});
答案 1 :(得分:0)
您在此处显示的代码中有一点错误是拼写错误getData()
调用不正确,因为您已使用getdata()
定义了您的函数。
我已经从你的代码中创建了一个fiddle,它的工作非常好。
angular.module('myApp', [])
.controller('myCtrl', function($scope, $http) {
$scope.sendPost = function() {
var data = $.param({
json: JSON.stringify({
name: $scope.newName
})
});
onLoad(data);
}
var getdata = function(data) {
$http.post("/echo/json/", data)
.success(function(data, status) {
console.log(data)
$scope.hello = data;
})
};
var onLoad = function(data) {
getdata(data);
console.log("load page");
};
$scope.hello = {
name: "Boaz"
};
$scope.newName = "";
})
此处显示的代码未产生此错误。请在解决此错误后用相关代码更新您的问题。
希望有所帮助