【发布时间】:2014-11-10 23:20:08
【问题描述】:
我无法弄清楚如何将参数从我的 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,据我了解,我不能在service 中使用$scope,所以我需要找到将$scope.formData 传递给服务的方法
很难的想法是,在控制器中,recipeService.saveRecipe($scope.formData);,但我不确定如何从服务中收集它,
当我更改服务this.saveRecipe(val) = save; 时它不起作用:(
任何帮助都会得到帮助
【问题讨论】:
-
Save 分配给 saveRecipe 时未定义。将分配放在保存功能之后。此外,您通常不需要手动构造 formData。通常会绑定到模型,然后将模型传递给 saveRecipe 函数
-
将变量作为参数传递
-
@pixelbits,感谢您的回答。我有一个模型可以绑定表单中的值。我想不通的是如何将模型传递给服务中的
saveRecipe函数?!
标签: angularjs angularjs-scope angularjs-service angularjs-controller