【发布时间】:2014-03-06 13:05:01
【问题描述】:
我是 AngularJS 的新手,不确定我错过了什么,尽管我知道这是我犯的一个小错误。请帮我解决以下情况。
我有一个表单,其中我写了一个帖子 {textArea} 并单击提交按钮,它调用 ng-click=createPost() 方法。
它转到controller.js,即:
app.controller('MyCtrl1', ['$scope', 'PostFactory', '$location', function ($scope, PostFactory, $location) {
/* callback for ng-click 'createUser': */
$scope.createPost = function() {
alert("in createPost" + $scope.post.postText);
alert("in createPost" + $scope.post);
PostFactory.create($scope.post)
$scope.posts.push($scope.post.postText);
$scope.post = "";
$location.path('/view1');
}
$scope.posts = PostFactory.query();
/*UserFactory.get({}, function (userFactory) {
$scope.firstname = userFactory.firstName;
})*/
}]);
而我的 service.js 是:
var services = angular.module('ngdemo.services', ['ngResource']);
//alert("In services");
services.factory('PostFactory', function ($resource) {
// alert("Reached services.js");
return $resource('/ngdemo/web/posts', {}, {
query: {
method: 'GET',
//params: {},
isArray: true
},
create: {method: 'POST'}
})
});
我的 Spring 控制器作为服务公开并具有 post 方法:
@RequestMapping(/*value = "/add",*/ method = RequestMethod.POST)
public @ResponseBody String addPost(@ModelAttribute(value = "") Post post, BindingResult result) {
System.out.println("Post value : " + post.getPostText());
//post.setPostId();
post.setPostTags("#dummy");
postService.addPost(post);
return "redirect:/";
}
我的表格:
<form novalidate="novalidate" class="form-horizontal">
<!-- Textarea -->
<div class="form-group">
<div class="col-md-4">
<textarea ng-model="post.postText" rows="4" cols="300" name="inputQuestion" id="post.postText" class="form-control expand" placeholder="What you want to pingle today?"></textarea>
</div>
<br>
<!-- Button -->
<div class="col-md-4">
<button ng-click='createPost()' class="btn btn-default plus"> Pingle it! </button>
</div>
</div>
</form>
问题是:控制器中的 Post 值总是为 null,尝试使用 $scope.post 和 $scope.postText 但没有乐趣!
请让我知道我在哪里失踪??????
更新:
我们如何在 controller.js 中将表单对象传递给 Spring Controller?帖子是我的域对象
【问题讨论】:
标签: angularjs rest angularjs-directive angularjs-scope angularjs-ng-repeat