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 badges2 Answers
Reset to default 4You 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
评论列表(0条)