我有3個加載與頁面相關的屬性的下拉列表。這些屬性是:樂器,風格,評分。這些屬性從服務調用中加載。
實施例使用儀器:
//Get Instruments
$http.get('/api/Instrument/GetAllInstruments').success(function (data, status, headers, config) {
$scope.instruments = data;
}).error(function (data, status, headers, config) {
$scope.error = "An Error has occured while fetching Instruments!" + data;
});
和HTML:
<select class="form-control"
name="recordInstrument"
data-ng-model="recordInstrument"
required
data-ng-options="i.ID as i.Description for i in instruments">
<option value="">-- Choose an Instrument --</option>
</select>
用戶可以從列表中選擇一個記錄,並在窗體上,該記錄的值被加載。這些值還包括爲下拉菜單設置所選值的屬性值。
當用戶點擊記錄時,會調用一個「編輯」功能。該函數調用獲取記錄的服務,然後執行if語句以確定記錄屬性數組是否爲空。如果屬性數組不爲空,那麼它會對數組執行forEach循環,設置ng模型,在這種情況下爲「$ scope.recordInstrument」,以便爲記錄的下拉列表設置選定的默認值。如果數組爲空,則將ng模型設置爲0,以將下拉菜單重置爲無記錄選定值「選擇儀器」。
下面是一段代碼: //編輯商店頁面 $ scope.edit =函數(){
if (this.page.SPPreambleID === null || this.page.SPPreambleID === 0) {
this.page.SPPreambleID = -1;
}
$http.get('/api/StorePage/GetStorePage?StorePageID=' +
this.page.StorePageID +
'&SPPreambleID=' +
this.page.SPPreambleID).success(function (data) {
$scope.updateShow = true;
$scope.addShow = false;
$scope.newpage = data;
if (data.SPAttributeRefID.length > 0) {
angular.forEach($scope.newpage.SPAttributeRefID, function (attribute, index) {
if (attribute == 1) {
$scope.instruments = $scope.instruments.reduce(function (result, instrument) {
result[instrument.ID] = instrument;
return result
}, {});
$scope.recordInstrument = $scope.instruments[data.AttributeID[0]].ID;
};
if (attribute == 2) {
$scope.style = $scope.styles.reduce(function (result, style) {
result[style.ID] = style;
return result
}, {});
$scope.recordStyle = $scope.styles[data.AttributeID[1]].ID;
};
if (attribute == 3) {
$scope.scoring = $scope.scorings.reduce(function (result, scoring) {
result[scoring.ID] = scoring;
return result
}, {});
$scope.recordScoring = $scope.scorings[data.AttributeID[2]].ID;
};
});
}
else {
$scope.recordInstrument = 0;
$scope.recordStyle = 0;
$scope.recordScoring = 0;
}
}).error(function() {
$scope.error = "An Error has occured while Editing this Store Page!" + data;
});
}
上面的代碼是工作之前,我就把如果行: 如果($ scope.newpage.SPAttributeRefID.length> 0){ ... }
我加入了,如果檢查數組的長度是因爲有些紀錄沒有下拉的屬性,如果我是從未來的原因之前的記錄,它有價值,然後下降這些數據將會保留在之前的記錄中。
我添加了if後,我開始收到指向reduce函數的錯誤,我不知道我在做什麼錯誤。
我想請求幫助,試圖解決這個問題。
非常感謝。
我想我貼我的代碼不正確並刪除了幾條我不應該在發佈之前減小它的目標。我更新了編輯功能的代碼,所以你可以看到,如果我添加了這個分號,那麼代碼就會被破壞,從而使其他條件不再出現。 –
@erasmocarlos啊是啊..我的錯誤。再看一下這一行,})。錯誤(函數()在我看來,)試圖關閉,當它不應該。 } .error(函數()可能是修復。) –
那個「)」。之前「錯誤(功能...」,是「this.page.SPPreambleID」)的一部分.success(功能(數據){...「 - 如果我刪除它,代碼中斷。 –