我有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(功能(数据){...“ - 如果我删除它,代码中断。 –