Javascript 如何在不硬编码的情况下以角度发送未定义的对象关键点

Javascript 如何在不硬编码的情况下以角度发送未定义的对象关键点,javascript,angularjs,Javascript,Angularjs,我在一个对象中有一个键列表,我试图在发送它们时检查其中哪些是未定义的。问题是如果我发送一个未定义的密钥,它将被删除。现在,我正在将它们硬编码到scope变量中 HTML: 这是可行的,但很难看,而且它仍然不会显示在后端。如果您需要定义键,即使值未定义,也可以在控制器中定义范围变量,并将设置为“”。然后,您可以检查是否为空,例如在php中使用if(空([your var])) 为了简化,当您提交数据时,数据在您的作用域上,因此函数可以只发送$scope.recipe数据 $scope.recipe

我在一个对象中有一个键列表,我试图在发送它们时检查其中哪些是未定义的。问题是如果我发送一个未定义的密钥,它将被删除。现在,我正在将它们硬编码到scope变量中

HTML:


这是可行的,但很难看,而且它仍然不会显示在后端。

如果您需要定义键,即使值未定义,也可以在控制器中定义范围变量,并将设置为
“”
。然后,您可以检查是否为空,例如在php中使用
if(空([your var]))

为了简化,当您提交数据时,数据在您的作用域上,因此函数可以只发送$scope.recipe数据

$scope.recipe = {alias: '', selectedCategory: '', description: '', instructions: ''};
$scope.createRecipe = function(){
    //send it to the server
    $http.post('url', $scope.recipe).then(successCallback, errorCallback);
};

<form ng-submit="createRecipe()">
    <label>Beer Name</label></br>
    <input ng-model="recipe.alias"></br>
    <label>Category</label></br>
    <select ng-options="beer.alias as beer.alias for beer in beerTypes" ng-model="recipe.selectedCategory" ng-change="filterByCategory(selectedCategory)">
    </select></br>
    <label>Description</label></br>
    <textarea ng-model="recipe.description" cols="50" rows="5" class="input-box"></textarea></br>
    <label>Recipe</label></br>
    <textarea ng-model="recipe.instructions" cols="50" rows="5" class="input-box"></textarea></br>
    <input type="submit" class="button-color btn btn-default"></br>
</form>
$scope.recipe={alias:'',selectedCategory:'',description:'',instructions:'';
$scope.createRecipe=函数(){
//将其发送到服务器
$http.post('url',$scope.recipe.),然后(successCallback,errorCallback);
};
啤酒名

类别

描述

配方



如果需要定义键,即使值未定义,也可以在控制器中定义范围变量并将设置为
“”
。然后,您可以检查是否为空,例如在php中使用
if(空([your var]))

为了简化,当您提交数据时,数据在您的作用域上,因此函数可以只发送$scope.recipe数据

$scope.recipe = {alias: '', selectedCategory: '', description: '', instructions: ''};
$scope.createRecipe = function(){
    //send it to the server
    $http.post('url', $scope.recipe).then(successCallback, errorCallback);
};

<form ng-submit="createRecipe()">
    <label>Beer Name</label></br>
    <input ng-model="recipe.alias"></br>
    <label>Category</label></br>
    <select ng-options="beer.alias as beer.alias for beer in beerTypes" ng-model="recipe.selectedCategory" ng-change="filterByCategory(selectedCategory)">
    </select></br>
    <label>Description</label></br>
    <textarea ng-model="recipe.description" cols="50" rows="5" class="input-box"></textarea></br>
    <label>Recipe</label></br>
    <textarea ng-model="recipe.instructions" cols="50" rows="5" class="input-box"></textarea></br>
    <input type="submit" class="button-color btn btn-default"></br>
</form>
$scope.recipe={alias:'',selectedCategory:'',description:'',instructions:'';
$scope.createRecipe=函数(){
//将其发送到服务器
$http.post('url',$scope.recipe.),然后(successCallback,errorCallback);
};
啤酒名

类别

描述

配方



我使用下划线来设置对象的默认值。这不是一个答案,因为它离题了——但仍然很方便
$scope.recipe.alias=alias | |“”
还可以尝试使用
ng value=”“
初始化它们
null
survives
JSON.stringify()
…我使用下划线来设置对象的默认值。这不是一个答案,因为它离题了——但仍然很方便
$scope.recipe.alias=alias | |“”
也尝试使用
ng value=”“
初始化它们
null
生存
JSON.stringify()
…我这样做了,但在后端它仍然没有显示未定义的变量。编辑:dandavis是正确的:未定义后端不会传输。我最初将它们设置为null,现在一切正常。减去这个,这个代码仍然有效。非常感谢。我这样做了,但在后端它仍然没有显示未定义的变量。编辑:dandavis是正确的:未定义后端不会传输。我最初将它们设置为null,现在一切正常。减去这个,这个代码仍然有效。非常感谢。
$scope.recipe = {alias: '', selectedCategory: '', description: '', instructions: ''};
$scope.createRecipe = function(){
    //send it to the server
    $http.post('url', $scope.recipe).then(successCallback, errorCallback);
};

<form ng-submit="createRecipe()">
    <label>Beer Name</label></br>
    <input ng-model="recipe.alias"></br>
    <label>Category</label></br>
    <select ng-options="beer.alias as beer.alias for beer in beerTypes" ng-model="recipe.selectedCategory" ng-change="filterByCategory(selectedCategory)">
    </select></br>
    <label>Description</label></br>
    <textarea ng-model="recipe.description" cols="50" rows="5" class="input-box"></textarea></br>
    <label>Recipe</label></br>
    <textarea ng-model="recipe.instructions" cols="50" rows="5" class="input-box"></textarea></br>
    <input type="submit" class="button-color btn btn-default"></br>
</form>