2
NG選項我在JavaScript中動態默認值
$scope.prefixes = [
{'id': 'Mr', 'name': 'Mr.'},
{'id': 'Mrs', 'name': 'Mrs.'}
];
這個結構,我得到,如果我的用戶是先生或太太,像這樣
JSON.parse(localStorageService.get("user")).prefix.
根據數據,我得到,當用戶登錄所以用戶的前綴總是動態的。
這是我收到如果用戶是先生還是女士,把它在一個範圍VAR,然後使用該範圍VAR在HTML中的選擇標籤
$scope.prefixes = [
{'id': 'Mr', 'name': 'Mr.'},
{'id': 'Mrs', 'name': 'Mrs.'}
];
$scope.readPrefixesInit;
for (i = 0; i < $scope.prefixes.length; i++) {
//if dynamic is equal with one of the ids, get the name
if ($scope.prefixes[i].id == JSON.parse(localStorageService.get("user")).prefix){
$scope.readPrefixesInit = $scope.prefixes[i].name;
}
}
設置的默認值的想法然後在HTML
<select required autofocus class="form-control"
ng-model="prefix"
ng-init="prefix = {{ readPrefixesInit }}"
ng-options="f.id as f.name for f in prefixes">
<option value="">Select Prefix</option>
</select>
這是行不通的。
我得到這個錯誤Syntax Error: Token '{' invalid key at column 12 of the expression [prefix = {{ readPrefixesInit }}] starting at [{ readPrefixesInit }}].
我得到我的下拉但一直沒有價值預選。
我該如何解決這個問題? 謝謝
你在哪裏定義「readPrefixes」?我只在你的代碼中看到一個「前綴」數組 – chris
@chris對不起,並感謝他的糾正。我編輯我的OP。 – slevin