对于NG-模型AngularJS默认值 [英] AngularJS default value for ng-model

查看:137
本文介绍了对于NG-模型AngularJS默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时有可能使NG的模型得到的默认值,如

Is it possible to make ng-models get default values, like ''?

我对我所使用的形式jQuery的连载功能,每当AA值不是present,它仍然会包括在序列化的数据,例如 {名称:'',年龄:''}

I have a form on which I have used jQuery's serialize function, and whenever a a value is not present, it will still include it in the serialized data, E.g. {name: '', age: ''}.

然而,当我使用尝试使用通过张贴角的 $ HTTP $范围越来越模型 ,它显示为未定义当它是空的。

However, when I use try using posting it by using Angular's $http and getting the model from $scope, it appears as undefined when it is empty.

推荐答案

您可以简单地定义你的视图中使用他们提前在范围模型的属性。

You can simply define the properties on the model in the scope in advance of using them in your view.

如果你显示你的控制器code我会告诉你,你需要更新将它们添加到的范围是什么。

If you show your controller code I'll show you what you need to update to add them to the scope.

这应该是这个样子:

在该定义的应用程序的js文件

In your js file that defined the app

angular.module("MyModule", []).controller('MyCtrl', ['$scope', function ($scope) {
    $scope.myModel = {name:""};
}]);

在你的HTML

<input type="text" ng-model="myModel.name"/>

注意没有全局变量我preFER /功能,所以我利用角度提供了一个不同的语法来避免这种情况,并允许缩小。

Note I prefer not having global variables/functions so I'm using a different syntax provided by Angular to avoid this and to allow for minification.

这篇关于对于NG-模型AngularJS默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆