在ASP.NET MVC中编程时,我习惯于使用部分视图进行CRUD操作。现在我正在使用Spring+AngularJS,我希望我能以类似的方式工作。到目前为止,通过使用angular ui路由器,我已经完成了创建/更新的一些基本部分。
例子:
布局.html
<!doctype html>
<html lang="en" ng-app="Example">
<head>
<meta charset="utf-8">
<title>Test</title>
</head>
<body>
<div class="container">
<nav class="row">
<ul class="nav nav-pills">
<li ui-sref-active="active"><a ui-sref="clients">Clients</a></li>
</ul>
</nav>
</div>
<div class="container">
<div class="row">
<div class="col-md-12" ui-view></div>
</div>
</div>
<script src="libs/jquery/dist/jquery.min.js"></script>
<script src="libs/angular/angular.min.js"></script>
<script src="libs/angular-ui-router/release/angular-ui-router.min.js"></script>
<script src="appjs/app.js"></script>
<script src="appjs/services.js"></script>
<script src="appjs/controllers/ClientController.js"></script>
<script src="appjs/filters.js"></script>
<script src="appjs/directives.js"></script>
</body>
</html>
/客户端/create.html
<h1>Create new client</h1>
<form name="clientForm">
<div ui-view></div>
</form>
/客户端/edit.html
<h1>Edit client</h1>
<form name="clientForm" ng-show="!loading">
<input type="hidden" name="id" ng-model="client.id" />
<div ui-view></div>
</form>
/client/createOrEdit.html
<div class="form-group" ng-class="{'has-error': clientForm.name.$invalid}">
<label for="name">Name:</label>
<input id="name"
name="name"
type="text"
ng-model="client.name"
class="form-control"
required />
<span class="help-block" ng-show="clientForm.name.$error.required">Name field is required</span>
</div>
<div class="form-group" ng-class="{'has-error': clientForm.address.$invalid}">
<label for="address">Address:</label>
<input id="address"
name="address"
type="text"
ng-model="client.address"
class="form-control"
required />
<span class="help-block" ng-show="clientForm.address.$error.required">Address field is required</span>
</div>
<button class="btn btn-primary btn-lg btn-block"
ng-click="addOrUpdate(client)"
ng-disabled="clientForm.$invalid || clientForm.$pristine">Save</button>
应用程序.js
App.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise("/");
$stateProvider
/* Client */
.state("clients", {
url : "/clients",
templateUrl : "clients/index",
controller : ClientListController
})
.state("clientCreate", {
abstract : true,
url : "/client",
templateUrl : "clients/create",
controller : ClientCreateController
})
.state("clientCreate.createOrEdit", {
url : "/create",
templateUrl : "clients/createOrEdit",
})
.state("clientEdit", {
abstract: true,
url : "/client/{id:[0-9]{1,9}}",
templateUrl : "clients/edit",
controller : ClientEditController
})
.state("clientEdit.createOrEdit", {
url : "/edit",
templateUrl : "clients/createOrEdit",
})
});
ClientController.js
var ClientListController = function($scope, $http) {
$scope.error = false;
$scope.loading = false;
$scope.getClientList = function() {
$scope.loading = true;
$http.get('clients/clientlist.json')
.success(function(clientList) {
$scope.clients = clientList;
$scope.loading = false;
})
.error(function() {
$scope.error = true;
$scope.loading = false;
});
}
$scope.getClientList();
};
var ClientCreateController = function($scope, $http, $location) {
$scope.client = {};
$scope.loading = false;
$scope.addOrUpdate = function(client) {
client.id = null;
$scope.loading = true;
$http.post('clients/createOrEdit', client)
.success(function() {
$scope.loading = false;
$location.path('/clients').replace(); //redirect to index
});
};
};
var ClientEditController = function($scope, $stateParams, $http, $location) {
$scope.client = {};
$scope.error = false;
$scope.loading = false;
$scope.getClientById = function(id) {
$scope.loading = true;
$http.get('clients/' + id + '/client.json')
.success(function(client) {
$scope.client = client;
$scope.loading = false;
})
.error(function() {
$scope.error = true;
$scope.loading = false;
});
}
$scope.addOrUpdate = function(client) {
$scope.loading = true;
$http.post('clients/createOrEdit', client)
.success(function() {
$scope.loading = false;
$location.path('/clients').replace(); //redirect to index
});
};
var selectedId = $stateParams.id;
$scope.getClientById(selectedId);
}
这是我目前的设置,我对它本身很满意。然而,当试图通过增加一点复杂性来扩展它时,我陷入了困境。
假设我们的客户可以有多个位置,所以现在每个客户都有一个位置列表。
我想做的是制作用于创建/编辑位置的部分视图,当编辑或创建客户端时,在客户端创建/编辑页面上显示客户端拥有的每个现有位置的视图、删除它们的能力以及添加新位置的能力。
在ASP.NET MVC中,我可以使用部分视图、BeginCollectionItem和一些jQuery来实现这一点。我应该如何用角度来处理这个问题?
我尝试使用ng repeat和ng include,但是当尝试添加/删除位置时,没有任何内容反映到ng repeat,而且我也不喜欢这种方法。
我认为点击按钮后没有发生任何事情的原因与我收到
Async XHR deprecated warning
使用insertLocation方法单击按钮时,在控制台中。
insertLocation()
看起来像这样
$scope.insertLocation = function() {
$scope.client.locations.push({});
}
并放置在create和edit控制器中,并且客户端var也被编辑为如下所示
$scope.client = {locations: []};
请注意。我并不是在寻找解决这种情况的方法(虽然这会有所帮助),而是更多的关于我的方法的建议。这是错的吗?如果是这样,正确的心态、正确的方法是什么?