// SERVICES
app.factory('searchFactory', ['$http', function($http) {
return $http.post("/api", { tag: "food" });
}]);
// CONTROLLERS
app.controller('MainController', ['$scope', 'searchFactory', function ($scope, searchFactory) {
$scope.submit = function () {
searchFactory.then(function(response) {
$scope.recipeData = JSON.parse(response.data);
});
};
// HTML
<form ng-submit="submit()">
<div class="form-group">
<input type="text" ng-model="recipeTag" class="form-control" />
<input type="submit" class="btn btn-primary" value="Find Recipes" />
</div>
</form>
有谁知道如何使用 ng-model 中的 $scope.recipeTag 来替换工厂中的“food”?我需要能够将表单输入作为参数传递到工厂中。
最佳答案
您需要创建一个需要工厂中参数的函数。示例:
var factory= {
post: function(customTag) {
return $http.post("/api", { tag: customTag });
}
};
return factory;
关于javascript - 如何将 ng-model/$scope 与 Angular Factory 和 Controller 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33726848/