好久没写了,一直在忙别的东西,但是想想,还是把之前的补充完整好了。给大家一个参考,也为自己留个备份。

  首先写一个Html作为内容载体,主要结构如下

  

<div ui-view="navbar" ng-cloak=""></div>

<div class="container">
<div class="well" ui-view="content"></div>
</div> <div class="footer">
<p >页脚</p>
</div>

  新建一个抽象路由用来放导航条,

$stateProvider.state('site', {
'abstract': true,
views: {
'navbar@': {
templateUrl: 'scripts/components/navbar/navbar.html',
controller: 'NavbarController'
}
}
});

  假设我们要操作的对象为article(文章)。我们要对article对象进行CRUD操作。

  在此假设该数据的数据结构为:article:{title:String,content:String};

  首先创建一个叫做article的文件夹表示这个文件夹里的内容都是对article的操作。

  然后创建一个叫做article.js的路由配置文件,文件内容大体如下

angular.module('MyApp')
.config(function ($stateProvider) {
$stateProvider
//定义一个展示文章列表的state
.state('article', {
//它的父状态定义为一个自定义的抽象状态。
parent: 'site',
//这个是访问该状态时候浏览器地址栏对应的url
url: '/articles',
//此处对应内容部分,设置模板文件以及控制器
views: {
'content@': {
templateUrl: 'scripts/app/article/articles.html',
controller: 'ArticlesController'
}
}
})
.state('article.detail', {
parent: 'site',
//{id}表示该路径接受的参数。参数名为id,例如访问/article/1,这个1就是此时的id
url: '/article/{id}',
views: {
'content@': {
templateUrl: 'scripts/app/article/article-detail.html',
controller: 'ArticleDetailController'
}
},
//
resolve: {
//url中的参数id存在于$stateParams中,此时调用了一个ArticleService用来获取文章数据
entity: ['$stateParams', 'Article', function($stateParams, Article) {
//获取id为路径传来id的stateParams.
return Article.get({id : $stateParams.id});
}]
}
})
.state('article.new', {
parent: 'article',
url: '/article/new',
//这里用来弹出一个模态框。引用了ui-bootstrap的组件uibModal,模态框将显示于parent状态对应的页面上。
onEnter: ['$stateParams', '$state', '$uibModal', function($stateParams, $state, $uibModal) {
$uibModal.open({
templateUrl: 'scripts/app/article/article-dialog.html',
controller: 'ArticleDialogController',
size: 'lg',
resolve: {
entity: function () {
return {
title:null,
content:null
};
}
}
}).result.then(function(result) {
//当模态框确定关闭时重新刷新页面进入article状态。(新增了数据)
$state.go('article', null, { reload: true });
}, function() {
//当模态框取消关闭时重新进入article状态。(未新增数据,所以不刷新)。
$state.go('article');
})
}]
})
.state('article.edit', {
parent: 'article',
url: '/article/{id}/edit',
onEnter: ['$stateParams', '$state', '$uibModal', function($stateParams, $state, $uibModal) {
$uibModal.open({
templateUrl: 'scripts/app/article/article-dialog.html',
controller: 'ArticleDialogController',
size: 'lg',
resolve: {
entity: ['Article', function(Article) {
return Article.get({id : $stateParams.id});
}]
}
}).result.then(function(result) {
$state.go('article', null, { reload: true });
}, function() {
//返回上个路由状态。
$state.go('^');
})
}]
})
.state('article.delete', {
parent: 'article',
url: '/article/{id}/delete',
onEnter: ['$stateParams', '$state', '$uibModal', function($stateParams, $state, $uibModal) {
$uibModal.open({
templateUrl: 'scripts/app/article/article-delete-dialog.html',
controller: 'ArticleDeleteController',
size: 'md',
resolve: {
entity: ['Article', function(Article) {
return Article.get({id : $stateParams.id});
}]
}
}).result.then(function(result) {
$state.go('article', null, { reload: true });
}, function() {
$state.go('^');
})
}]
})
});

  接下来依次来看每个状态对应的controller和temmplate。

  1.首先来看articles.html

  此处用到一个uib-pagination组件,为ui-bootstrap的组件,用来做分页条使用,一般列表项多时大都需要采取分页展示。

<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>title</th>
<th>content</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="article in articles track by article.id">
<td>{{article.id}}</td>
<td>{{article.title}}</td>
<td>{{article.content}}</td>
<td>
<button type="button"
               ui-sref="article.detail({id:article.id})"
class="btn btn-info btn-sm">
<span class="glyphicon glyphicon-eye-open"></span>
</button>
<button type="button"
               ui-sref="article.edit({id:article.id})"
class="btn btn-primary btn-sm">
<span class="glyphicon glyphicon-pencil"></span>
</button>
<button type="button"
               ui-sref="article.delete({id:article.id})"
class="btn btn-danger btn-sm">
<span class="glyphicon glyphicon-remove-circle"></span>
</button>
</td>
</tr>
</tbody>
</table>
</div>
<div class="text - center">
<uib-pagination class="pagination - sm" total-items="totalItems" ng-model="page" ng-change="loadAll()"></uib-pagination>
</div>

   ArticlesController主要内容如下

'use strict';

angular.module('MyApp')
.controller('ArticleController', function ($scope, $state) {
$scope.articles = [];
$scope.page = 1;
$scope.loadAll = function() {
Article.query({page: $scope.page - 1, size: 20}, function(result, headers) {
$scope.links = ParseLinks.parse(headers('link'));
$scope.totalItems = headers('X-Total-Count');
$scope.articles = result;
});
};
$scope.loadPage = function(page) {
$scope.page = page;
$scope.loadAll();
};
$scope.loadAll(); })

  2.详情页面html如下:

<form id="fm" name="fm" novalidate>
<formly-form model="formModel" fields="formFields" options="formOptions" form="fm"> </formly-form>
<button onclick="history.go(-1);">返回</button>
</form>

  控制器内容如下,主要用了formly-form组件,具体细节不做介绍,用的时候查文档:  

'use strict';

angular.module('MyApp')
.controller('ArticleDetailController', function ($scope, $state) {
$scope.formModel = {};
$scope.formFields = [];
$scope.formOptions = {};
})

  3.编辑界面如下:

<form id="fm" name="fm" novalidate ng-submit="onFormSubmit(fm)">
<formly-form model="formModel" fields="formFields" options="formOptions" form="fm">
<div class="row form-submit-btn-group">
<div class="col">
<button type="submit" form="fm" class="btn btn-info">保存</button>
</div>
</div>
</formly-form>
<button onclick="history.go(-1)">返回</button>
</form>

  对应的控制器如下:

angular.module('MyApp')
.controller('ArticleEditController', function ($scope, $state) {
$scope.formModel = {};
$scope.formFields = [{
key: 'title',
type: 'inline-input',
templateOptions: {
label:"标题",
type:'text',
focus:true,
placeholder: '请输入标题',
required: true
}
}];
$scope.formOptions = {}; var editSuccess = function(){
console.log("edit ok");
} var editFailed = function(){
console.log("edit failed");
} $scope.onFormSubmit = function(){
var data = $scope.formModel; }
})

  4.增加界面如下:

<form id="fm" name="fm" novalidate ng-submit="onFormSubmit(fm)">
<formly-form model="formModel" fields="formFields" options="formOptions" form="fm">
<div class="row form-submit-btn-group">
<div class="col">
<button type="submit" form="fm" class="btn btn-info">提交</button>
</div>
</div>
</formly-form>
</form>

  对应的控制器如下:

'use strict';

angular.module('MyApp')
.controller('ArticleAddController', function ($scope, $state) {
$scope.formModel = {};
$scope.formFields = [{
key: 'title',
type: 'inline-input',
templateOptions: {
label:"标题",
type:'text',
focus:true,
placeholder: '请输入标题',
required: true
}
}];
$scope.formOptions = {}; var addSuccess = function(){
console.log("add ok");
} var addFailed = function(){
console.log("add failed");
} $scope.onFormSubmit = function(){
var data = $scope.formModel; }
})

  5.删除界面如下:

<form name="deleteForm" ng-submit="confirmDelete()">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"
ng-click="clear()">&times;</button>
<h4 class="modal-title">Confirm delete operation</h4>
</div>
<div class="modal-body">
<p>Are you sure you want to delete this article?</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal" ng-click="clear()">
<span class="glyphicon glyphicon-ban-circle"></span>&nbsp;<span>Cancel</span>
</button>
<button type="submit" ng-disabled="deleteForm.$invalid" class="btn btn-danger">
<span class="glyphicon glyphicon-remove-circle"></span>&nbsp;<span>Delete</span>
</button>
</div>
</form>

  对应的控制器如下:

angular.module('MyApp')
.controller('ArticleDeleteController', function ($scope, $state) {
var delSuccess = function(){
console.log("del success");
}
var delFailed = function(){
console.log("del failed");
} $scope.confirmDelete = function(){ }
})

0007-一套完整的CRUD_DEMO的更多相关文章

  1. Web自动化框架之五一套完整demo的点点滴滴(excel功能案例参数化+业务功能分层设计+mysql数据存储封装+截图+日志+测试报告+对接缺陷管理系统+自动编译部署环境+自动验证false、error案例)

    标题很大,想说的很多,不知道从那开始~~直接步入正题吧 个人也是由于公司的人员的现状和项目的特殊情况,今年年中后开始折腾web自动化这块:整这个原因很简单,就是想能让自己偷点懒.也让减轻一点同事的苦力 ...

  2. Egret是一套完整的HTML5游戏开发解决方案

    Egret是一套完整的HTML5游戏开发解决方案.Egret中包含多个工具以及项目.Egret Engine是一个基于TypeScript语言开发的HTML5游戏引擎,该项目在BSD许可证下发布.使用 ...

  3. 基于springboot+bootstrap+mysql+redis搭建一套完整的权限架构【六】【引入bootstrap前端框架】

    https://blog.csdn.net/linzhefeng89/article/details/78752658 基于springboot+bootstrap+mysql+redis搭建一套完整 ...

  4. 一套完整的VI包含哪些元素

    VI设计,即视觉识别系统,企业VI设计是企业品牌建设的重中之重.最近很多人都在问,一套完整的企业VI设计都包括哪些内容?笔者站在一个高级设计师的角度,来简单谈一谈VI设计包括哪些内容.文中指出,一套完 ...

  5. EasyRTMP+EasyDSS实现一套完整的紧急视频回传直播与存储回放方案

    需求来源 紧急视频回传云端:即拍即传.云端存储.紧急录像.云拍云录!这些需求现在可能对于我们来说比较远,大部分也是在行业中才会用到,但相信在不就的将来肯定会落地到每个人的手中,因为这是一个自我保护.自 ...

  6. 分享Node.js + Koa2 + MySQL + Vue.js 实战开发一套完整个人博客项目网站

    这是个什么的项目? 使用 Node.js + Koa2 + MySQL + Vue.js 实战开发一套完整个人博客项目网站. 博客线上地址:www.boblog.com Github地址:https: ...

  7. 部署一套完整的Kubernetes高可用集群(二进制,v1.18版)

    一.前置知识点 1.1 生产环境可部署Kubernetes集群的两种方式 目前生产部署Kubernetes集群主要有两种方式: kubeadm Kubeadm是一个K8s部署工具,提供kubeadm ...

  8. 部署一套完整的Kubernetes高可用集群(二进制,最新版v1.18)下

    七.高可用架构(扩容多Master架构) Kubernetes作为容器集群系统,通过健康检查+重启策略实现了Pod故障自我修复能力,通过调度算法实现将Pod分布式部署,并保持预期副本数,根据Node失 ...

  9. SpringBoot-06:SpringBoot增删改查一套完整的考试案例

    本此博客记录一套考试题,随后我把项目以及题目发到github上,简单的说一下springboot的开发 本此考试题用Spring+SpringMVC+MyBatis+SpringBoot+MySQL+ ...

随机推荐

  1. if条件判断

    if 条件判断的是布尔值,常用的有以下几种 1.in 在不在它里面,返回的是布尔值 names='zhangsan lisi xiaoming' print("zhangsan" ...

  2. 用angularjs的$http提交的数据,在php服务器端却无法通过$_REQUEST/$_POST获取到

  3. iOS如何限制使用SDK的版本? 解决iOS项目的版本兼容问题

      更新 2015-11-16 感谢微博好友@zyyy_000的评论,补充了为什么要在+ (void)load方法里面做Method Swizzling. 前言 最近,在做项目时,因为某种原因,突然要 ...

  4. iOS开发-Realm数据库

    Realm Realm-Object-c,见:https://realm.io/cn/docs/objc/latest/Realm官网:https://realm.io 使用流程 导入头文件#impo ...

  5. Linux运行环境搭建(一)——安装JDK

    下载Linux版jdk 官网:http://www.oracle.com/technetwork/java/javase/downloads/index.html 解压并拷贝到指定目录 tar zxv ...

  6. 今天需要做手机端访问的页面,所以把meta的整理一下。

    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale= ...

  7. html 添加flash标签 兼容firefox ie google

    <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="550" heig ...

  8. __weak、__strong这样的关键词和weak、strong有哪些区别

    ios4 设备上最好就不要使用 ARC... strong,weak 用来修饰属性.strong 用来修饰强引用的属性:@property (strong) SomeClass * aObject;  ...

  9. 关于java日期

    时间戳转为yyyy-MM-dd SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String formatDa ...

  10. 《DSP using MATLAB》示例 Example 6.25

    代码: % x = [-0.9, 0.81]; [y, L, B] = QCoeff(x, 3) % Unquantized parameters r = 0.9; theta = pi/3; a1 ...