【发布时间】:2023-03-31 02:30:02
【问题描述】:
我在我的项目中使用 angular x-editable。 http://vitalets.github.io/angular-xeditable/#editable-row
除了显示验证错误外,其他一切正常。 这是我的 HTML 模板:
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Width</th>
<th>Length</th>
<th>Sheets quantity</th>
<th>Low price</th>
<th>High price</th>
<th>Currency</th>
<th></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="material in sheetMaterials">
<td>
<span editable-text="material.name" e-name="name" e-form="form" e-required>
{{ material.name }}
</span>
</td>
<td>
<span editable-text="material.width" e-name="width" e-form="form" e-required>
{{ material.width }}
</span>
</td>
<td>
<span editable-text="material.length" e-name="length" e-form="form" e-required>
{{ material.length }}
</span>
</td>
<td>
<span editable-text="material.sheets" e-name="sheets" e-form="form" e-required>
{{ material.sheets }}
</span>
</td>
<td>
<span editable-text="material.priceLow" e-name="priceLow" e-form="form" e-required>
{{ material.priceLow }}
</span>
</td>
<td>
<span editable-text="material.priceHigh" e-name="priceHigh" e-form="form" e-required>
{{ material.priceHigh }}
</span>
</td>
<td>
<span editable-select="material.currency"
e-ng-options="s.value as s.text for s in currencies"
e-name="currency"
e-form="form"
e-required>
{{ showCurrency( material ) }}
</span>
</td>
<td style="white-space: nowrap">
<form editable-form name="form"
onaftersave="updateSheetMaterial( $data, material._id, form )"
ng-show="form.$visible"
class="form-buttons form-inline"
shown="inserted == material">
<button type="submit"
ng-disabled="form.$waiting"
class="btn btn-primary">
Save
</button>
<button type="button"
ng-disabled="form.$waiting"
ng-click="form.$cancel()"
class="btn btn-default">
Cancel
</button>
</form>
<div class="buttons" ng-show="!form.$visible">
<button class="btn btn-primary" ng-click="form.$show()">
Edit
</button>
<button class="btn btn-danger" ng-click="removeSheeteMaterial( materials, $index )">
Delete
</button>
</div>
</td>
</tr>
</tbody>
</table>
<button class="pull-right btn btn-primary" ng-click="createSheetMaterial()">Add</button>
这是我处理表单行为的控制器:
angular.module( 'client' )
.controller(
'materialController',
[
'$scope',
'$filter',
'sheetMaterialFactory',
function(
$scope,
$filter,
sheetMaterialFactory
){
/**
* index action
* @return void
*/
$scope.index = function(){
$scope.currencies = [
{ value: 'RUB', text: "Р" },
{ value: 'EUR', text: "€" },
{ value: 'USD', text: "$" },
]
sheetMaterialFactory.getList().then( function( materials ){
$scope.sheetMaterials = materials;
});
$scope.content = "partials/material.html";
}
$scope.showCurrency = function( material ){
var selected = $filter('filter')( $scope.currencies, { value: material.currency });
return ( material.currency && selected.length ) ? selected[ 0 ].text : 'Not set';
}
/**
* update sheet material
* @param data – object of material options
* @param _id – unique id of material
* @return void
*/
$scope.updateSheetMaterial = function( data, _id, form ){
data._id = _id;
var action = data._id ? "update" : "create";
sheetMaterialFactory
[ action ]( data )
.then( function( sheetMaterial ){
if( "update" == action ){
var collection = $scope.sheetMaterials;
collection = collectionService.updateObject( collection, sheetMaterial );
} else {
collection.push( sheetMaterial );
}
}, function( error ){
if( error.data.errors ){
angular.forEach( error.data.errors, function( errorData, field ){
form.$setError( field, errorData.message );
});
} else {
form.$setError( 'name', 'Неизвестная ошибка' );
}
});
}
/**
* create sheet material
* @return void
*/
$scope.createSheetMaterial = function( data ){
if( !data ){
var sheetMaterial = { name: "Some name" };
$scope.sheetMaterials.push( sheetMaterial );
return;
}
}
$scope.index();
}
]
);
我检查了所有的小细节,发现 form.$setError 完美无缺。真正分配给表单元素的错误文本。但提交表单后不显示。如果有人知道如何解决它 - 非常感谢您的回复。
【问题讨论】:
标签: javascript angularjs x-editable