如何在Angular中发送未定义的对象关键帧而不进行硬编码
How send undefined object keys in Angular without hardcoding them
我在一个对象中有一个键的列表,当我发送它们时,我试图检查其中哪些是未定义的。问题是,如果我发送一个未定义的密钥,它将被删除。现在,我正在将它们硬编码到scope变量中。
HTML:
<form ng-submit="createRecipe(recipe.alias, recipe.selectedCategory, recipe.description, recipe.instructions)">
<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.createRecipe = function(alias, selectedCategory, description, instructions){
$scope.recipe.alias = alias;
$scope.recipe.selectedCategory = selectedCategory;
$scope.recipe.description = description;
$scope.recipe.instructions = instructions;
//Then I send it to the server
}
这是有效的,但它很难看,而且它仍然没有出现在后端。
如果需要定义键,即使值未定义,也可以在控制器中定义范围变量,并将设置为""
。然后后端可以检查是否为空,例如在php中使用if (empty([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>
相关文章:
- 关键帧之间的css3动画延迟
- 访问css3动画的关键帧
- 当用户滚动到页面的某个部分时,如何触发关键帧
- 动态关键帧插入在 IE 或 Firefox 上不起作用
- CSS3卡翻转带关键帧不工作
- 一种读取或更改CSS动画关键帧的方法
- Skrollr:当滚动位置在最后一个关键帧之后时传递函数
- 替换对象数组中的关键帧
- 如何在Javascript中更改关键帧状态
- CSS - 在关键帧中触发悬停
- 多次单击时运行关键帧动画
- CSS3 关键帧动画是否比属性过渡更平滑?
- 使用关键帧图像旋转器时,图像不会在两个块之间同步协调
- CSS关键帧过渡的最佳方法
- CSS 关键帧动画的随机“起点”
- 为什么单击时的关键帧动画只触发一次
- CSS3 关键帧不起作用
- 使用 JQuery 事件检测特定的 CSS 关键帧动画
- 多次使用 javascript 触发 CSS3 关键帧
- 如何在Angular中发送未定义的对象关键帧而不进行硬编码