Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 默认情况下,如何将ng模型值设置为空字符串_Javascript_Angularjs_Angularjs Scope_Angularjs Ng Model - Fatal编程技术网

Javascript 默认情况下,如何将ng模型值设置为空字符串

Javascript 默认情况下,如何将ng模型值设置为空字符串,javascript,angularjs,angularjs-scope,angularjs-ng-model,Javascript,Angularjs,Angularjs Scope,Angularjs Ng Model,这是我的密码: <div ng-app="myApp" ng-controller="myCtrl"> Name: <input ng-model="myInput.name" /> age: <input ng-model="myInput.age" /> <pre> {{myInput | json}} </pre> </div> <script type=

这是我的密码:

 <div ng-app="myApp" ng-controller="myCtrl">
    Name: <input ng-model="myInput.name" />
    age: <input ng-model="myInput.age" />

    <pre>
      {{myInput | json}}
    </pre>
  </div>

  <script type="text/javascript">
    angular.module('myApp', [])
      .controller('myCtrl', function($scope) {
        $scope.myInput = {};
      });
  </script>

姓名:
年龄:
{{myInput | json}
angular.module('myApp',[])
.controller('myCtrl',函数($scope){
$scope.myInput={};
});
默认情况下,空字符串未设置为ng模型

默认情况下,如何将所有myInput值设置为“”

这是你的电话号码

更新:

假设有超过100个myInput字段。是否需要手动将其设置为“”

更新2:


Pankaj Parkar指令运行良好。但当然,它将所有模型值都设置为“”。如何仅在模型为空时将模型设置为空字符串?我选中了attrs.value==undefined,但没有任何帮助。

您可以使用object literal初始化变量
myInput
。这将确保它们在加载时设置为空字符串

Object.keys($scope.myInput).forEach(function(key, index) {
   $scope.myInput[key] = '';
});
编辑:如果要初始化
$scope.myInput
对象中的所有值,可以使用以下JS代码:

.controller('myCtrl', function($scope) {
    $scope.myINput = {
        name: '',
        age: ''
    };
});

或者您可以使用一些库,例如
下划线
lodash
来迭代
myInput
对象中的值。我假设
myInput
对象中所有值的数据类型都是
String
。如果存在其他数据类型,如
Array
,则可以在forEach()块中插入该逻辑,并相应地初始化值。

有两种方法

在控制器中:

Name: <input ng-model="myInput.name" ng-init="myInput.name=''" />
age: <input ng-model="myInput.age" ng-init="myInput.age=''" />
还是在视野中

var attributes = Object.keys(myInput);

for (i = 0; i < attributes.length; i++){
   myInput[attributes[i]] = '';  
}
名称:
年龄:

您可以使用以下代码获取密钥:

input empty-input ng-model="myInput.name" /> <br> <br>
age: <input empty-input ng-model="myInput.age" />
var attributes=Object.keys(myInput);
对于(i=0;i
然后循环遍历属性并将它们分配给空值,然后将它们重新分配回myJSONObject。
我假设您的对象是JSON对象

您可以有一个指令,负责将默认值设置为
'

标记

.directive('emptyInput', function($parse) {
    return {
      require: '?ngModel',
      link: function(scope, element, attrs, ngModel) {
        //ngModel should be there & it should be of type text
        if (angular.isObject(ngModel) &&
          (!attrs.type || attrs.type === 'text')) {
          var model = $parse(attrs.ngModel);
          model.assign(scope, '');
        }
      }
    }
});
另一件事是,您可以循环对象,并将每个属性设置为
'
。但这有一个限制,即对于某些属性,您不想将值更改为
'
。那么,最好使用指令方式,而不是在循环中添加条件

仅当模型为空时才将模型设置为空字符串


我想你把
myINput
放错控制器了。。。它应该是
$scope.myInput={}
在控制器中,您将
myInput
设置为数组。在HTML中,您使用
myInput
作为对象。请纠正你的拼写。这是一个数组还是一个对象?@Moid Mohd。对不起,我弄错了。请现在看。@georgeawg。它可以是对象或对象数组Use
angular.forEach
,将在数组和对象上工作。你能纠正我哪里出错吗?因为
$scope.myInput={}
在您的情况下为空,因此没有可用的密钥。您需要使用键填充scope变量,然后将其值初始化为空字符串。非常感谢您提供此指令。您好。还有一个问题。如何仅在模型为空时将模型设置为空字符串?我包括attrs.value==未定义,但没有帮助。
.directive('emptyInput', function($parse) {
    return {
      require: '?ngModel',
      link: function(scope, element, attrs, ngModel) {
        //ngModel should be there & it should be of type text
        if (angular.isObject(ngModel) &&
          (!attrs.type || attrs.type === 'text')) {
          var model = $parse(attrs.ngModel);
          model.assign(scope, '');
        }
      }
    }
});
.directive('emptyInput', function ($parse) {
            return {
                require: '?ngModel',
                link: function (scope, element, attrs, ngModel) {
                    var ngModelGet = $parse(attrs.ngModel);
                    scope.$watch(attrs.ngModel, function () {
                        if (ngModelGet(scope) == undefined && angular.isObject(ngModel) && (!attrs.type || attrs.type === 'text')) {
                            var model = $parse(attrs.ngModel);
                            model.assign(scope, '');
                        }
                    });
                }
            }
        });