我將用戶輸入傳遞給控制器函數,但空字符串不聲明對象屬性。ng-model未定義爲空字符串?
<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:''}
};
有沒有辦法強制空中傳遞空字符串時創建的對象屬性?
訪問當然,但這是唯一的方法嗎?沒有辦法強制它在飛行中? –
@DanKanze沒有其他對象將被初始化,只有當你編輯文本框 –
ng-init = {data:{location:'',radius:''}「會做的伎倆,但它在控制器 – Mephiztopheles