AngularJS:将params从控制器传递到服务

时间:2014-09-17 03:28:32

标签: angularjs angularjs-scope angularjs-service angularjs-controller

我在确定如何将参数从angular controller传递给我时遇到了问题 service

#my controller  
'use strict';

angular.module('recipeapp')
  .controller('recipeCtrl', ['$scope', 'recipeService',
    function($scope, recipeService){
      $scope.recipeFormData={};
      $scope.recipeSave = function(){
        recipeService.saveRecipe();
      }


  }]);

#my service
'use strict';
angular.module('recipeapp').service('recipeService',['$http', function($http){

  this.saveRecipe = save;

  function save(callback){
     //calling external http api
  }

}]);

我在这里尝试做的是,从我的表单和控制器获取$scope.formData应该将其传递给service,根据我的理解,我无法使用$scopeservice内,所以我需要找到一种方法将$scope.formData传递给服务

艰难的想法会在控制器中recipeService.saveRecipe($scope.formData);,但我不确定如何从服务中收集,

当我更改服务this.saveRecipe(val) = save;时,它不起作用:(

任何帮助都是适当的

2 个答案:

答案 0 :(得分:19)

此示例演示了角度应用的正确结构:

  • 控制器内的模型初始化
  • 实施服务单例,并注入控制器
  • 使用$ http promises异步调用Web API调用,并允许服务的调用者处理其成功/失败。
  • 使用“控制器作为”语法从控制器公开函数,而不是直接从作用域公开函数。
  • 双向数据模型绑定(文本框到配方和配方到文本框)

在您的控制器中初始化您的模型:

angular.module('recipeapp')
  .controller('recipeCtrl', ['$scope', 'recipeService',
    function($scope, recipeService){
      // initialize your model in you controller
      $scope.recipe={};

      // declare a controller function that delegates to your service to save the recipe
      this.saveRecipe = function(recipe) {
           // call the service, handle success/failure from within your controller
           recipeService.saveRecipe(recipe).success(function() { 
               alert('saved successfully!!!'); 
           }).error(function(){
               alert('something went wrong!!!');
           });

      }
  }]);

在配方服务中,定义saveRecipe功能:

angular.module('recipeapp').service('recipeService',['$http', function($http){

  // expose a saveRecipe function from your service
  // that takes a recipe object
  this.saveRecipe = function(recipe){
      // return a Promise object so that the caller can handle success/failure
      return $http({ method: 'POST', url: '/api/recipe/add', data: recipe});
  }

}]);

将食谱对象绑定到您的视图;添加一个按钮来调用saveRecipe控制器功能并保存配方(传入模型配方对象):

<div ng-app="recipeapp" ng-controller="recipeCtrl as ctrl">
   <form name="recipeForm">
    Recipe Name: <input type="text" ng-model="recipe.name" />
    <button ng-click="ctrl.saveRecipe(recipe)">Save Recipe</button>
    </form>
</div>

答案 1 :(得分:1)

var module = angular.module('example.service', []);


module.services('ExampleServices', ['$http', '$q', function ($http,
$q) {

    var resourceUrl;

    return {


        setResourceUrl: function(resourceUrl) {
            this.resourceUrl = resourceUrl;
        },



        create: function(params) {
            //access params here sent from controller 
           //make call to server using $http 
           //return back the promise or response
        },



        remove: function(id) {
            //access id here sent from controller 
           //make call to server using $http 
           //return back the promise or response
        }

}

稍后在您的控制器中注入服务ExampleServices

然后访问:

ExampleServices.create(params)

params可以是任何对象,很可能是使用表单捕获的数据。

ExampleServices.remove(id)

id可以是要从数据库中删除的记录的主ID。

希望有所帮助:)