[英]Two way databinding not working in angularjs
I am trying to have one very simple CRUD app. 我想要一个非常简单的CRUD应用程序。 It involves one form with single text box. 它涉及一个带有单个文本框的表单。 and all the entries posted through box shall display in a grid below text box. 通过框发布的所有条目都应显示在文本框下方的网格中。
All is well but somehow grid doesn't update with new entry unless page is refreshed. 一切都很好,但除非刷新页面,否则网格不会以新条目更新。 I am using loopback api running on localhost:3000 and my angular app on localhost:9000. 我正在使用localhost:3000上运行的loopback api和localhost:9000上的角度应用程序。 Database is MySQL. 数据库是MySQL。
Same code works as expected if I am using MEAN stack. 如果我使用MEAN堆栈,相同的代码按预期工作。 Now we have to support mySQL and decoupled API from my application. 现在我们必须从我的应用程序支持mySQL和解耦API。 This is controller. 这是控制器。
angular.module('dashboardApp')
.controller('StateCtrl', function ($scope, $http) {
$scope.formData = {};
$scope.baseUrl = '//localhost:3000/api/v1/states';
$http.get($scope.baseUrl).success(function(states) {
$scope.states = states;
});
$scope.create = function() {
$http.post($scope.baseUrl, $scope.formData)
.success(function(states) {
$scope.states = states;
})
.error(function(states) {
console.log('Error: ' + states);
});
};
});
This is view. 这是观点。
<form class="form-inline" role="form" data-ng-submit="create()">
<div class="form-group">
<label class="sr-only" for="name">State</label>
<input type="text" class="form-control" id="name" placeholder="Enter state" ng-model="formData.name">
</div>
<button type="submit" class="btn btn-default">Enter</button>
</form>
<table class="table table-striped">
<tr ng-repeat="state in states">
<td>{{state.name}}</td>
</tr>
</table>
Any help is appreciated. 任何帮助表示赞赏。 just a note: I've tried using services/resources as well instead of $http. 只是一个注释:我已经尝试过使用服务/资源而不是$ http。
I think the problem here is that $http.get
is returning an array and $http.post
in the $scope.create
function is returning a single object. 我觉得这里的问题是, $http.get
是返回一个数组和$http.post
在$scope.create
函数返回一个对象。
$scope.states
array. 您必须将返回的对象推送到$scope.states
数组中。 Or .. 要么 .. Return an array from the $http.post
request and assign it to $scope.states
从$http.post
请求返回一个数组并将其分配给$scope.states
angular.module('dashboardApp') .controller('StateCtrl', function ($scope, $http) { $scope.formData = {}; $scope.baseUrl = '//localhost:3000/api/v1/states'; angular.module('dashboardApp')。console('StateCtrl',function($ scope,$ http){$ scope.formData = {}; $ scope.baseUrl ='// localhost:3000 / api / v1 / states' ;
$http.get($scope.baseUrl).success(function(states) { $scope.states = states; // this returns an array }); $scope.create = function() { $http.post($scope.baseUrl, $scope.formData) .success(function(states) { //$scope.states = states; // this return an object // Do this $scope.states.push(states); }) .error(function(states) { console.log('Error: ' + states); }); };
}); });
NB: Or you could just return the whole array on $http.post
注意:或者你可以在$http.post
上返回整个数组
As you are doing Cross-Origin Resource Sharing (CORS) is the API service adding the required HTTP Access-Control-Allow-Origin header? 在进行跨源资源共享 (CORS)时,API服务是否添加了所需的HTTP Access-Control-Allow-Origin标头? If not the response will be blocked by the browser. 如果不是,浏览器将阻止响应。 Adding an error callback to your $http.get()
should also help detect what is wrong. 向$http.get()
添加错误回调也应该有助于检测错误。
I concur with @Maurice that you might be running into a cross domain issue you have two ports involved 9000 and 3000. If the data you are expecting back is in json you can try adding this your url 我同意@Maurice您可能遇到跨域问题,您有两个端口涉及9000和3000.如果您期待的数据是在json,您可以尝试添加此URL
$scope.jsonpUrl = $scope.baseUrl+"?callback=JSON_CALLBACK";
$http.jsonp($scope.jsonpUrl)
.success(function(states) {
$scope.states = states;
})
.error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
console.log("Error:", data);
});
Give it a try and let us know. 试一试,让我们知道。 Cheers 干杯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.