Javascript 角度ng模型formdata不工作
这是我的控制器:Javascript 角度ng模型formdata不工作,javascript,angularjs,Javascript,Angularjs,这是我的控制器: var expenses = angular.module('expenses', []); expenses.controller('mainController', function($scope, $http){ $scope.formData = {}; $http.get('/api/transactions').then(function(data){ $scope.transactions = data.data; console.log(da
var expenses = angular.module('expenses', []);
expenses.controller('mainController', function($scope, $http){
$scope.formData = {};
$http.get('/api/transactions').then(function(data){
$scope.transactions = data.data;
console.log(data.data);
}, function(error){
console.log(error)
});
$scope.createTransaction = function() {
console.log($scope.formData)
$http.post('/api/transactions', $scope.formData).then(function(data) {
$scope.formData = {};
$scope.transactions = data.data;
console.log(data.data);
}, function(data) {
console.log('Error: ' + data);
});
};
})
//js
angular
.module('app', [])
.controller('ctrl', function($scope){
console.log('created')
$scope.formData = {};
$scope.createTransaction = function(){
console.log($scope.formData.shop);
}
})
这是我的html:
<div id="mdlInputExpense" class="modal">
<div class="modal-content">
<h4>Create a new expense</h4>
<div class="row">
<form class="col s12" ng-submit="createTransaction()">
<div class="row">
<div class="input-field col s6">
<input placeholder="Shop" id="shop" type="text" class="validate autocomplete" ng-model="formData.shop">
<label for="shop">Shop</label>
</div>
<div class="input-field col s6">
<input placeholder="Category" id="category" type="text" class="validate autocomplete" ng-model="formData.category">
<label for="category">Category</label>
</div>
</div>
<div class="row">
<div class="input-field col s12">
<input placeholder="Amount of money" id="amount" type="number" class="validate" ng-model="formData.amount">
<label for="amount">Amount of money</label>
</div>
</div>
<button type="submit" class="waves-effect waves-green btn-flat">Submit</a>
</form>
</div>
</div>
<div class="modal-footer">
<a href="#!" class="modal-action modal-close waves-effect waves-green btn-flat">Submit</a>
</div>
</div>
//js
angular
.module('app', [])
.controller('ctrl', function($scope){
console.log('created')
$scope.formData = {};
$scope.createTransaction = function(){
console.log($scope.formData.shop);
}
})
我一看就知道它很好用。这里是jsbin的链接:
//html JS-Bin 创建新的费用 商店 类别 金额 提交 提交
//js
angular
.module('app', [])
.controller('ctrl', function($scope){
console.log('created')
$scope.formData = {};
$scope.createTransaction = function(){
console.log($scope.formData.shop);
}
})
在绑定视图之前,您还没有创建$scope.formData属性吗?。在
开发人员工具控制台的网络选项卡中显示哪些内容?还要确保它的空日志是console.log(JSON.stringify($scope.formData))
,因为开发人员工具会更新登录realtime@Leguest在“网络”选项卡中,我没有看到帖子发生,只有“事后获得”。为什么会有“事后获得”?您在提交处理程序中在何处调用get请求?@Leguest发布后,我从服务器获取结果并返回它们。