【发布时间】:2014-03-07 22:08:45
【问题描述】:
我有一个表单,在其中我使用 ng-click 提交文本,现在它调用休息服务并插入数据库 - 工作正常。同时,在同一个函数中,它调用了另一个休息服务,但什么也没发生,我想知道如何将参数传递给服务???
Controller.js
app.controller('MyCtrl1', ['$scope', 'PostFactory', '$location', function ($scope, PostFactory, $location) {
/* callback for ng-click 'createUser': */
$scope.createPost = function () {
alert("in createPost" + $scope.UserPost.postText);
alert("in createPost" + $scope.UserPost);
PostFactory.postmain.create($scope.UserPost)
$scope.allposts.push($scope.UserPost);
$scope.UserPost = "";
$scope.allresultsfinal = PostFactory.allresults.query({tag: $scope.UserPost.postText})
$location.path('/view1');
}
$scope.allposts = PostFactory.postmain.query();
/*UserFactory.get({}, function (userFactory) {
$scope.firstname = userFactory.firstName;
})*/
}]);
Services.js
'use strict';
/* Services */
var services = angular.module('ngdemo.services', ['ngResource']);
//alert("In services");
services.factory('PostFactory', ['$resource', function ($resource) {
return {
postmain: $resource('/ngdemo/web/posts', {}, {
query: {method: 'GET', isArray: true },
create: {method: 'POST'}
}),
allresults: $resource('/ngdemo/web/posts/result/:tag', {id: '@id'}, {
query: {method: 'GET', params: {id: '@id'}, isArray: true },
create: {method: 'POST'}
})};
}]);
在所有结果中,id 没有出现,或者我不知道 controll 是否到达这里。在“postmain”中发布提交并且查询工作正常。请告诉我该怎么做?
我需要发送“标签”,即“#scope.UserPost.postText”作为控制器中的参数
$scope.allresultsfinal = PostFactory.allresults.query({tag: $scope.UserPost.postText})
到 service.js "allresults" 并从 rest 服务中取回数据。
【问题讨论】:
-
你的意思是你想让 URL 形成为“/ngdemo/web/posts/result/:tag?id=someValue”,如果是,id 的值是什么
-
对不起,它不是 id,它的标签,而不是 id,它应该是标签
-
url 应该是 /ngdemo/web/posts/result/:tag 其中标签是我将通过查询参数发送的字符串
标签: angularjs rest angularjs-directive angularjs-scope angularjs-ng-repeat