javascript - ng-model undefined with empty strings? - Stack Overflow

I am passing user input to a controller function but empty strings do not declare object properties.<

I am passing user input to a controller function but empty strings do not declare object properties.

<form>
  <input type="text" ng-model="data.location" />
  <input type="text" ng-model="data.radius" />
  <button  type="button" ng-click="getSearch(data)">Search</button>
</form>

$scope.getSearch = function(data) {
  console.log(data);
  //undefined
  //...but what if I want {location:'', radius:''}
};

Is there a way to force the object properties to be created when passing empty strings on the fly?

I am passing user input to a controller function but empty strings do not declare object properties.

<form>
  <input type="text" ng-model="data.location" />
  <input type="text" ng-model="data.radius" />
  <button  type="button" ng-click="getSearch(data)">Search</button>
</form>

$scope.getSearch = function(data) {
  console.log(data);
  //undefined
  //...but what if I want {location:'', radius:''}
};

Is there a way to force the object properties to be created when passing empty strings on the fly?

Share Improve this question asked May 31, 2013 at 13:57 Dan KanzeDan Kanze 18.6k28 gold badges84 silver badges136 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

You should be able to initialize data.location and data.radius in your controller to '', at least that way they aren't undefined.

You should access it as $scope.data and not just data.

Also you don't need to pass it as argument to your getSearch(). models are bound to $scope and should be accessed via $scope

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744400959a4572384.html

相关推荐

  • javascript - ng-model undefined with empty strings? - Stack Overflow

    I am passing user input to a controller function but empty strings do not declare object properties.<

    6天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信