如何在一个ng-click函数中传递两个范围

时间:2018-01-05 02:09:29

标签: angularjs

我想通过ng-click功能createRecipe()传递配方和成分范围。我把这些成分称为数组。

$scope.ingredients = [];
$scope.recipe = {};

然后在我的html中,我有输入有$ index

<input type="text" class="form-control" id="RecipeName" name="RecipeName" required ng-model="recipe.Name">

<input type="text" class="form-control" id="IngredientQuantity" name="IngredientQuantity" ng-model="ingredients.Quantity[$index+1]" />

然后当用户点击提交时,我需要获得两个范围

<input type="submit" value="Create" class="btn btn-default" ng-click="createRecipe(recipe;ingredients)"/>

我将在我的食材表中引用recipeid,所以我需要一个功能。

$scope.createRecipe = function (recipe) {
    $scope.uploader.uploadAll();
    $http({
        method: 'POST',
        url: '/Recipes/CreateRecipe',
        data: $scope.recipe
    }).then(function (response) {
        var recipeID = response.data;
        var vmRecipeIng = {
            Quantity: $scope.ingredients.Quantity,
            UOM: $scope.ingredients.UOM,
            Name: $scope.ingredients.Name,
            RecipeId: recipeID
        };
        vmRecipeIngs = vmRecipeIng;
        alert(vmRecipeIng);
        console.log(vmRecipeIng);
        $http({
            method: 'POST',
            url: '/RecipeIngredients/CreateRecipeIngredient',
            data: vmRecipeIng
        }).then(function (response) {
        }, function () { alert('Error CreateRecipeIngredient'); });
    }, function () { alert('Error CreateRecipe'); });
};

1 个答案:

答案 0 :(得分:2)

由于你的HTML传递了两个参数,你的函数需要有两个参数,

$scope.createRecipe = function (recipe,ingredient) {
}

该函数的参数也应该由 , 而不是 ;

分隔
<input type="submit" value="Create" class="btn btn-default" ng-click="createRecipe(recipe,ingredients)">