Angularjs 使用$http发布$scope变量
JSAngularjs 使用$http发布$scope变量,angularjs,Angularjs,JS $scope.SendData = function () { var req = { method: 'POST', url: 'http://localhost:8080/ajouterPr', headers: { 'Content-Type': 'application/json' }, data: {'nom':$scope.nom, 'poste':$scope.po
$scope.SendData = function () {
var req = {
method: 'POST',
url: 'http://localhost:8080/ajouterPr',
headers: {
'Content-Type': 'application/json'
},
data: {'nom':$scope.nom, 'poste':$scope.poste}
}
$http(req).then(function(){
alert('success');
}, function(){
alert(error.error);
});
};
拜托,谁能告诉我这有什么问题!!为什么我不能在数据中使用$scope.var?从
SendData
函数中删除$scope
参数。您不需要添加它,因为它已经在控制器的上下文中可用,通过添加它,您正在SendData
的内部创建一个名为$scope
的新变量,该变量未定义,因为调用它时没有传入任何内容
$scope.SendData = function () {
var req = {
method: 'POST',
url: 'http://localhost:8080/addPr',
headers: {
'Content-Type': 'application/json'
},
data: {'nom':$scope.poste, 'poste':$scope.name}
}
$http(req).then(function(){
alert('success');
}, function(){
alert(error.error);
});
};
编辑
尝试将$scope
变量设置为对象。您还需要制作ng model=“foo.poste”
和ng model=“foo.name”
$scope
是否在函数所在的控制器中初始化?您希望发生什么?到底发生了什么?有什么错误吗?控制台显示什么?$scope.nom=“”$范围。poste=“”$scope.SendData=function($scope){var req={method:'POST',url:'',头:{'Content Type':'application/json'},数据:{'nom':$scope.poste,'poste':$scope.nom}}$http(req)。然后(function(){alert('success');},函数(){alert(error.error);});};angular.js:13283 TypeError:无法读取m.$scope.SendData(app.js:35)处未定义的属性“poste”@IssamDalhoumi请尝试在下面发布的答案Wood catch,我在borked代码格式中遗漏了这一点。@IssamDalhoumi您必须定义“不工作”。错误更改了吗?现在的错误是什么?你从哪里调用$scope.SendData
?@IssamDalhoumi在你的上一条评论中,这是非常令人困惑的,你的SendData
签名中仍然有$scope
参数…@IssamDalhoumi你是否将信息插入$scope.poste
和$scope.name
?
$scope.foo = {
poste : "poste",
name: "name"
}
$scope.SendData = function () {
var req = {
method: 'POST',
url: 'http://localhost:8080/addPr',
headers: {
'Content-Type': 'application/json'
},
data: {'nom':$scope.foo.poste, 'poste':$scope.foo.name}
}
$http(req).then(function(){
alert('success');
}, function(){
alert(error.error);
});
};