![](/img/trans.png)
[英]Array_Diff is deleting only the key/value from the array of objects but not the whole object
[英]ngModel, set a whole object and not only its value from ngOptios
我只是,無法弄清楚如何設置我的ng-model
包含選擇的值,而不是對象本身。
HTML
<select name="status"
data-ng-model="search.status"
data-ng-options="s.label for s in statuses">
</select>
$scope.statuses = [
{ label: ' - si/no - ', value: '' },
{ label: ' Migrated ', value: 0 },
{ label: ' Active ', value: 1 },
{ label: ' Canceled ', value: 2 },
{ label: ' Un-confirmed ', value: 3 },
];
$scope.search.status = $scope.statuses[0];
結果將是:
stdClass Object
(
[label] => - si/no -
[value] =>
)
現在,我只需要[value]
,而不是整個對象。 我該如何實現?
PS我不想訪問object->value
,我只想讓我的x
變量包含已經包含的值。
我以前有這個,它正在工作:
<select name="status" class="txt" data-ng-model="search.status">
<option value=""> - ALL - </option>
<option value="0"> Migrated </option>
<option value="1"> Active </option>
<option value="2"> Canceled </option>
<option value="3"> Un-confirmed </option>
</select>
我的search.status
僅包含選定的值,而不包含像上面的代碼那樣得到的整個對象。
您需要更改標記。 ng-options
表達式中的as
關鍵字令人贊嘆。
下面的表達式將對象屬性s.value
設置為模型,但s.label
仍保留為<option>
元素的標簽(我想這就是您想要的)。
<select name="status"
ng-model="search.status"
ng-options="s.value as s.label for s in statuses">
</select>
當然,必須修改控制器中的模型值初始化(如@Orangepill所述):
$scope.search.status = $scope.statuses[0].value;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.