AngularJS:一个 'partial form' 用于创建和更新文章
AngularJS: One 'partial form' for both create and update article
我们以一个简单的表单为例,一个文本输入。我希望它是部分的,这样我就可以向它添加其他输入,并为创建和更新艺术家提供一种形式。
部分
<input name="name" type="text" data-ng-model="artist.name" id="name">
创建表单
<section data-ng-controller="ArtistsController">
<form name="artistForm" class="form-horizontal" data-ng-submit="create()" novalidate>
<ng-include src="'/modules/artists/views/partials/form-artist.client.view.html'"></ng-include>
</form>
</section>
编辑(或更新)表单
<section data-ng-controller="ArtistsController" data-ng-init="findOne()">
<form name="artistForm" class="form-horizontal" data-ng-submit="update(artistForm.$valid)" novalidate>
<ng-include src="'/modules/artists/views/partials/form-artist.client.view.html'"></ng-include>
</form>
</section>
在我的客户端控制器中,我可以创建一个艺术家 :
$scope.create = function() {
var artist = new Artists({
name: this.name
});
artist.$save(function(response) {
//success!
}, function(errorResponse) {
$scope.error = errorResponse.data.message;
});
}
我可以更新艺术家的名字:
$scope.update = function() {
var artist = $scope.artist;
artist.$update(function() {
//success!
}, function(errorResponse) {
$scope.error = errorResponse.data.message;
});
};
问题是当我创建艺术家时,this.name
未定义。如果我将 parial 中的 data-ng-model 更改为 data-ng-model="name"
,我可以正确地创建一个艺术家,但输入不会填充更新表单中的名称。
关于如何将两种形式正确合并为一个部分有什么想法吗?
正如我所见,在创建的情况下,您还没有将艺术家对象添加到范围中。你需要将艺术家添加到 $scope。
在部分你应该使用 artist.name.
该值实际上存储在范围内的 artistForm 对象中。
你只需要改变:
name: this.name
至
name: $scope.artistForm.name.$viewValue
PS: console.log($scope) 对调查有很大帮助
我们以一个简单的表单为例,一个文本输入。我希望它是部分的,这样我就可以向它添加其他输入,并为创建和更新艺术家提供一种形式。
部分
<input name="name" type="text" data-ng-model="artist.name" id="name">
创建表单
<section data-ng-controller="ArtistsController">
<form name="artistForm" class="form-horizontal" data-ng-submit="create()" novalidate>
<ng-include src="'/modules/artists/views/partials/form-artist.client.view.html'"></ng-include>
</form>
</section>
编辑(或更新)表单
<section data-ng-controller="ArtistsController" data-ng-init="findOne()">
<form name="artistForm" class="form-horizontal" data-ng-submit="update(artistForm.$valid)" novalidate>
<ng-include src="'/modules/artists/views/partials/form-artist.client.view.html'"></ng-include>
</form>
</section>
在我的客户端控制器中,我可以创建一个艺术家 :
$scope.create = function() {
var artist = new Artists({
name: this.name
});
artist.$save(function(response) {
//success!
}, function(errorResponse) {
$scope.error = errorResponse.data.message;
});
}
我可以更新艺术家的名字:
$scope.update = function() {
var artist = $scope.artist;
artist.$update(function() {
//success!
}, function(errorResponse) {
$scope.error = errorResponse.data.message;
});
};
问题是当我创建艺术家时,this.name
未定义。如果我将 parial 中的 data-ng-model 更改为 data-ng-model="name"
,我可以正确地创建一个艺术家,但输入不会填充更新表单中的名称。
关于如何将两种形式正确合并为一个部分有什么想法吗?
正如我所见,在创建的情况下,您还没有将艺术家对象添加到范围中。你需要将艺术家添加到 $scope。 在部分你应该使用 artist.name.
该值实际上存储在范围内的 artistForm 对象中。
你只需要改变:
name: this.name
至
name: $scope.artistForm.name.$viewValue
PS: console.log($scope) 对调查有很大帮助