angular 项目回顾
从学习angular,到实际项目开发不到一周,完全是边写边学呀,都是为了项目,已使用angular 开发了两个项目了,有些技术当时只是会用,都没好好回顾一下,现在有时间回顾一下,项目中用到的一些指令,服务,路由,filter 等,
一点点记录一来
// 初始化 angular.bootstrap(dom,['appName']);
//html 转化
// 需传参 $sce
$scope.escape = function(html) {
return $sce.trustAsHtml(html);
};
// html
<div ng-bind-html="escape(data)"></div>
// http
$http({
method:'get', // post ....
url:'/Service/resume', // url
params: {id: uid}
}).success(function(data){
console.log(data)
})
// filter
.filter('getCity', function(){
return function(city){
return $.parseJSON(city).city;
}
});
//html
{{city | getCity}}
//标签切换
<span class="{{curShow=='register'?'current':''}}" ng-click="switchView('register')">个人注册</span>
<span class="{{curShow=='login'?'current':''}}" ng-click="switchView('login')">个人登录</span>
<div class="{{curShow!='register'?'hide':''}}">
// register
</div>
<div class="{{curShow!='login'?'hide':''}}">
//login
</div>
//初始化
$scope.curShow = 'register';
$scope.switchView = function(view) {
$scope.curShow = view;
}
//ng-click="switchView('login')"
<div class="jd">
<label " checked="checked" id="company">企业</label>
<label " id="personl">个人</label>
</div>
//radio 切换
<div class="jd">
<div ng-show="isCheckboxSelected('1')">
<label for="leader"><input type="radio" name="guanxi" id="leader">主管</label>
<label for="hr"><input type="radio" name="guanxi" id="hr">HR</label>
</div>
<div ng-show="isCheckboxSelected('2')">
<label for="workmate"><input type="radio" name="guanxi" id="workmate">同事</label>
<label for="students"><input type="radio" name="guanxi" id="students">同学</label>
<label for="friend"><input type="radio" name="guanxi" id="friend">朋友</label>
</div>
</div>
$scope.checkboxSelection = ';
$scope.isCheckboxSelected = function(index) {
return index === $scope.checkboxSelection;
};
// factory
var app = angular.module('factory',[]);
app.factory('testFactory', function () {
// return {
// lable: function(){
// return [
// {'id' : 1, 'name':'Ted', 'total': 5.996},
// {'id' : 2, 'name':'Michelle', 'total': 10.996},
// {'id' : 3, 'name':'Zend', 'total': 10.99},
// {'id' : 4, 'name':'Tina', 'total': 15.996}
// ];
// }
// }
// * edit new methods
var data = [
{, 'name':'Ted', 'total': 5.996},
{, 'name':'Michelle', 'total': 10.996},
{, 'name':'Zend', 'total': 10.99},
{, 'name':'Tina', 'total': 15.996}
];
var factory = {};
factory.lable = function(){
return data;
}
return factory;
});
app.controller('TestController',function($scope,testFactory){
$scope.customers = testFactory.lable();
})
// 代码详见,github
// https://github.com/llqfront/angular/tree/master/angular
// service.js
var app = angular.module('factory',[]);
app.factory('testFactory', function ($http) {
var factory = {};
factory.lable = function(){
return $http.get('/js/test.json');
}
return factory;
});
// controller.js
app.controller('TestController',function($scope,testFactory){
function init(){
testFactory.lable().success(function(data){
$scope.customers = data;
})
}
init();
})
//service、provider、factory写法
var app = angular.module('appName', []);
app.service('testService',function(){
this.lable = 'this is service';
});
app.factory('testFactory', function () {
return{
lable: function(){
return 'this is factory';
}
}
});
app.provider('testProvider', function(){
this.$get = function(){
return 'this is provider';
}
});
<body ng-controller='outputCtrl'>
<p>{{ output1 }}</p>
<p>{{ output2 }}</p>
<p>{{ output3 }}</p>
</body>
var app = angular.module('appName');
app.controller('outputCtrl', function($scope,testService, testFactory, testProvider){
$scope.output1 = testService.lable;
$scope.output2 = testFactory.lable();
$scope.output3 = testProvider;
});
require('lib/angular-route');//引入 route
var app = angular.module('testApp',['ngRoute','factory','ctrl']);// ngRoute
app.config(function($routeProvider) {
$routeProvider.when('/', {
templateUrl: '/view/index.html', // 模板路径
controller: 'TestController' // 模板 中的 controller
})
.when('/book', {
templateUrl: '/view/book.html',
controller: 'BookController'
})
.when('/test', {
templateUrl: '/view/test.html',
controller: 'txController'
})
.otherwise({
redirectTo:'/'
});
});
今天呢在这增加一条,很多人看官网,或找资料,也包括我自己就是一个显示隐藏的功能怎么就实现不了呢
angular 显示 隐藏 ng-show ng-hide
笔者在这写几个例子你就会明白了,非repeat 内的 可以这样理解
//html
<p class="show-olive-btn" ng-click="showDetail(olive)">btn</p>
<div class="test" ng-show="olive.oliveDetail">
test
</div>
// controller
$scope.olive = {};
$scope.showDetail = function(olive){
olive.oliveDetail = ! olive.oliveDetail;
}
// 测试代码 html
{{olive}}<br/>
{{olive.oliveDetail}}
当你放上这两行就可以看出
默认olive.oliveDetail 是没有值,也相当于默认为false
当点击时 值为true false 切换
相当于改变{"oliveDetail":false}{"oliveDetail":true}
试一下就能明白了
有人说,单个的好实现如果在repeat 中如何实现呀
笔者也写一个例子
<tr ng-repeat="olive in customers | filter:searchText">
<td>
<p class="show-olive-btn" ng-click="showDetail(olive)">btn</p>
{{olive}} <!-- {"id":1,"name":"Ted","total":5.996,"oliveDetail":true/false} -->
{{olive.oliveDetail}}<!-- true/false -->
<div class="test" ng-show="olive.oliveDetail">
test
</div>
</td>
</tr>
//controller
$scope.showDetail = function(olive){
olive.oliveDetail = ! olive.oliveDetail;
}
写法其实是一样的只是 这次的olive 变成了 ng-repeat="olive in customers" 中单项了
如果还不懂,写出来试一下就知道了
tack by $index
watch
...
待续....
angular 项目回顾的更多相关文章
- 个人从源码理解angular项目在JIT模式下的启动过程
通常一个angular项目会有一个个模块(Module)来管理各自的业务,并且必须有一个根模块(AppModule)作为应用的入口模块,整个应用都围绕AppModule展开.可以这么说,AppModu ...
- 把angular项目整合到.net mvc中
之前的开发选择的是完全舍弃服务端,仅保留最简单web服务器提供angular经打包的静态资源,此外所有的业务与数据请求都访问一个分离的WebApi来实现.不过最近碰到一个需求,有必要使用多个客户端,而 ...
- Angular20 nginx安装,angular项目部署
1 nginx安装(Windows版本) 1.1 下载安装包 到官网下载Windows版本的nginx安装包 技巧01:下载好的压缩包解压即可,无需安装 1.2 启动nginx 进入到解压目录,点击 ...
- Angular4--提速--提升Angular项目的首页打开速度(包含微信登录优化)
Angular项目的首页打开速度很慢,有时候会有几秒的加载时间.如果在手机端访问的话,怕是要等待十多秒,这对用户体验很差.下面参考http://www.cnblogs.com/feiyu159/p/8 ...
- Angular4---部署---将Angular项目部署到IIS上
---恢复内容开始--- Angular项目部署到一个IIS服务器上 1.安装URL rewrite组件: 网址:https://www.microsoft.com/en-us/download/de ...
- 网站开发进阶(二十一)Angular项目信息错位显示问题解决
Angular项目信息错位显示问题解决 绪 最近在项目开发过程中遇到这样一个棘手的问题:查询出所有订单信息后,点击选择某一个订单,查询出的结果是上一次查询所得的结果.而且会出现点击两次才可以显示订单详 ...
- [转]Angular4---部署---将Angular项目部署到IIS上
本文转自:https://www.cnblogs.com/kingkangstudy/p/7699710.html Angular项目部署到一个IIS服务器上 1.安装URL rewrite组件: 网 ...
- [转]Angular项目目录结构详解
本文转自:https://blog.csdn.net/yuzhiqiang_1993/article/details/71191873 版权声明:本文为博主原创文章,转载请注明地址.如果文中有什么纰漏 ...
- angularjs探秘<三> 控制器controller及angular项目结构
先来看一个例子 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset=&quo ...
随机推荐
- 瞬间从IT屌丝变大神——命名规则
为了避免命名冲突,命名规则如下: 公共组件因为高度重用,,命名从简,不要加前缀. 各栏目的相应代码,需加前缀,前缀为工程师姓名拼音的首字母,例如:海子前缀为“hz_”,分隔符为下划线"_&q ...
- Python之Tkinter模块学习
本文转载自:http://www.cnblogs.com/kaituorensheng/p/3287652.html Tkinter模块("Tk 接口")是Python的标准Tk ...
- java 复习003 之排序篇
由java 复习003跳转过来的C语言实现版见some-sort-algorithms 快速排序(不稳定 O(n log n)) package vell.bibi.sort_algorithms; ...
- homework -06 围棋
playPrev功能的实现 public void playPrev(ref GoMove gm) { Point p = gm.Point; m_colorToPlay = gm.Color; cl ...
- 获取iOS设备信息(内存/电量/容量/型号/IP地址/当前WIFI名称)
1.获取电池电量(一般用百分数表示,大家自行处理就好) 1 2 3 4 -(CGFloat)getBatteryQuantity { return [[UIDevice current ...
- 【转】CocoaPods的安装以及遇到的坑
一.CocoaPods是什么? CocoaPods是一个用Ruby写的.负责管理iOS项目中第三方开源库的工具,CocoaPods能让我们集中的.统一管理第三方开源库,为我们节省设置和更新第三方开源库 ...
- UVALive 7281 Saint John Festival (凸包+O(logn)判断点在凸多边形内)
Saint John Festival 题目链接: http://acm.hust.edu.cn/vjudge/contest/127406#problem/J Description Porto's ...
- Spring EL Lists, Maps example
In this article, we show you how to use Spring EL to get value from Map and List. Actually, the way ...
- Spring init-method and destroy-method example
In Spring, you can use init-method and destroy-method as attribute in bean configuration file for be ...
- 使用https时,网站一些内容不能正常显示的问题
在网站开发过程中,使用http网站页面一切正常. 但改成https后,发现网站一些页面不能正常显示出来,比如看上去没有样式等. 原因是: 在程序中调用了比如JQuery,而引用的URL使用的是Http ...