Angular1.0
公司会议室组织分享,两个小时困死我了,一点凌乱笔记:
$http.get和promise一样有then方法,成功,失败
jquery each遍历对象i,n ng-app
ng-controller 数据绑定库 ng-config ng-run
ng-module 一些内部模块,已经封装好的功能 ng-bind和{{}}异曲同工共 里面加竖线过滤,内部js语法兼容
与$scope的关系 ng-include就相当于data-html引用外部html
ng-pattern 可以用正则(就是ng-module没有的,比如tel,可以自己定义)
ng-show ng-repeat 重复 x in arr,中间用x.键值,x就是某i对应的
<tr ng-repeat="x in arr">
<td>{{x."time"}}</td>
<td>{{x."name"}}</td>
<td>{{x."price"}}</td>
</tr>
遍历数组 x in x ng-class
ng-minlength 用于input规范长度
ng-maxlength <select>的optionn遍历
已选中的 selectedNames 只要是select标签上 ng-module写的值
外面还是要套<form> <input>的欸不加requited
type="Email"
$dirty 判断器
$valid 判断器
$invalid 判断器
$pristine
$error.reqired 判断
$error.email 判断
以下是在codecademy做的练习,做完几乎全忘。。
UNIT 1: YOUR FIRST APP Lesson: Your First App
Get up and running quickly by building an AngularJS app from scratch. Quiz: Your First App
Try this Codecademy Pro Quiz free!
PRO TRIAL
Project: Bolt Network 1
In this project, you'll create a movie review board using a controller and a view. Project: Pizza Planet
In this project, you'll create a restaurant menu using filters and directives. Project: MOVE Log
In this project, you'll create an exercise tracking app using directives. UNIT 2: DIRECTIVES Lesson: Directives
Learn how to use directives to make standalone UI components.
Quiz: Directives
Project: Bolt Network 2
In this project, you'll add custom directives to your movie review board. Project: Gameboard
In this project, you'll create a custom directive that displays the score of a game. Project: Feedster
In this project, you'll build a news feed using custom directives. UNIT 3: SERVICES
Lesson: Services Use services to communicate with a server.
Quiz: Services
Project: Outbox 1
In this project, you'll build an email app. Project: Top 10
In this project, you'll fetch movie data from the server and display it in a custom directive. UNIT 4: ROUTING Lesson: Routing Add routes to build powerful single-page applications.
Quiz: Routing
Project: Outbox 2
In this project, you'll add to your email app by mapping URLs to views. Project: Calendar
In this project, you'll create a calendar app with routes for each view. Project: Reader
In this project, you'll create an e-reader app. UNIT 5: PUTTING IT ALL TOGETHER Project: NearMe 1
In this project, you'll build a location-based service using a third-party custom directive. Project: NearMe 2
In this project, you'll fetch data from the Wikipedia API and display it in your map. Project: NearMe 3
In this project, you'll map URLs to different views in your app using routing. ANGULARJS FINAL PROJECT
Exclusive for Pro: AngularJS Final Project
In this project you will create a web application using AngularJS. The app, Suggestion Box, will allow you to gather suggestions, upvote and comment on them. You’ll leave Codecademy’s learning environment and build locally, on your own computer, testing and running your web application in the browser.
AngularJS is a framework for building dynamic web apps
基本结构
So far this is our typical workflow when making an AngularJS app:
- Create a module, and use
ng-app
in the view to define the application scope.(在app.js中定义模块) - Create a controller, and use
ng-controller
in the view to define the controller scope.(在controller中丰满模块内的变量值) - Add data to
$scope
in the controller so they can be displayed with expressions in the view.(在html中插值引用这些变量)
另外有一个angular最小例子:https://github.com/glitchtank/angular-seed-master
过滤器 filter
AngularJS comes with a few more built-in filters. Let's use two more.
In MainController.js inside$scope.product
, add a third property named pubdate
:
pubdate: new Date('2014', '03', '08')
我对third property的理解确实没有错,确实就是这么写的啊!可能拼写有误没有通过run,最后get code一看究竟:
$scope.product = {
name: 'The Book of Trees',
price: 19,
pubdate: new Date('2014', '03', '08')
}
<p class="date">
, display the product's pubdate
.pubdate
by piping it to the date
filter.name
by piping it to the uppercase
filter.<p class="title"> {{ product.name | uppercase }} </p>
<p class="price"> {{ product.price | currency }} </p>
<p class="date"> {{ product.pubdate | date }} </p>
原来所谓过滤器就是在html里面 | 号后面写的类型,比如date代表四位的日期,number代表数字。
html模板的循环adapter
<div ng-repeat="product in products" class="col-md-6">
<div class="thumbnail">
<img src="img/the-book-of-trees.jpg">
<p class="title">{{ product.name }}</p>
<p class="price">{{ product.price | currency }}</p>
<p class="date">{{ product.pubdate | date }}</p>
</div>
</div>
就能把
[
{
name: 'The Book of Trees',
price: 19,
pubdate: new Date('2014', '03', '08'),
cover: 'img/the-book-of-trees.jpg'
},
{
name: 'Program or be Programmed',
price: 8,
pubdate: new Date('2013', '08', '01'),
cover: 'img/program-or-be-programmed.jpg'
}
]
这样的数组给刷进去。
directives(自定义标签)
ng-app
,ng-controller
, ng-repeat
, ng-src, ng-click
...What can we generalize about directives? Directives bind behavior to HTML elements. When the app runs, AngularJS walks through each HTML element looking for directives.
$scope.plusOne = function(index){
$scope.products[index].likes += 1;
}
<p class="likes" ng-click="plusOne($index)">
做完绑定事件以后就出现了喜闻乐见的Congratulations!
- A user visits the AngularJS app.
- The view presents the app's data through the use of expressions, filters, and directives. Directives bind new behavior HTML elements.
- A user clicks an element in the view. If the element has a directive, AngularJS runs the function.
- The function in the controller updates the state of the data.
- The view automatically changes and displays the updated data. The page doesn't need to reload at any point.
这张图倒是挺抢眼的:
ng-repeat 的用法:
<div ng-repeat="product in products">
<img ng-src="{{ product.cover }}">
<p class="title">{{ product.name }}</p>
</div>
这里很有趣的是product in products 的语法,其实controller中只定义了products,就去掉s写xxx in xxxs就行,
后面如果要用info模板,<app-info info="app"></app-info>
这个模板的命名就是appInfo用“-”拆开来,这个驼峰式也是Angular内约定俗成的, 它的定义方法就是下面一节的app.directive("appInfo"),function{...
Restrict
- A 用于元素的 Attribute,这是默认值
- E 用于元素的名称 (The
'E'
means it will be used as a new HTML element.) - C 用于 CSS 中的 class
自定义一段html模板嵌入到index.html
app.directive('appInfo', function() {
return {
restrict: 'E',
scope: {
info: '='
//scope specifies pass information into this directive through an attribute named info
//The '='
tells the directive to look for an attribute named info in the<app-info>
},
templateUrl: 'js/directives/appInfo.html'
}; });
就像ui.framework的updateView一样,引用方便简单:<app-info info="move"></app-info>
模板内部只需要html片段(含ng directive自定义标签和插值变量)。
当定义的directive方法带有传入的参数时:
app.directive('installApp', function() {
return {
restrict: 'E',
scope: {
},
templateUrl: 'js/directives/installApp.html' ,
link: function(scope, element, attrs) {
scope.buttonText = "Install",
scope.installed = false, //设了标识flag来切换两种按钮文字 scope.download = function() {
element.toggleClass('btn-active'); //加上hover效果
if(scope.installed) {
scope.buttonText = "Install";
scope.installed = false; //改变标识flag的值
} else {
scope.buttonText = "Uninstall";
scope.installed = true; //改变标识flag的值
}
};
},
};
});
这些参数是Angular处理传入的,就像ajax请求的回调函数,可以选择接收或者不接收。
服务 Services
AngularJS's built-in $http
封装后的用法是:
app.factory('forecast', ['$http', function($http) {
return $http.get('https://s3.amazonaws.com/codecademy-content/courses/ltp4/forecast-api/forecast.json')
.success(function(data) {
return data;
})
.error(function(err) {
return err;
});
}]);
controller在接收service对象的时候也是可选参数。通常只需要一个$scape,现在传入一个名为forecast的服务:
app.controller('MainController', ['$scope', function($scope,'forecast') {
//上面传入的forecast就是在单独的service文件中定义的服务名,注意要用字符串
}]);
目标:Display a day's four pieces of data. Use the date
filter to format the datetime
.
注意一个插值变量的写法 {{ day.datetime | date }}
前面是数据,后面是类型。
迁移 Routing
有过实战经验就知道routing是做应用时很重要的一环,我们公司搞了个坑死爹的State Machine来代参照和替代app.route,每次牵着这二货走道都很费劲。
在angular中,用ng-view(div写在head里面即可)引入routeProvider
app.js中初始化module后配置:
app.config(function ($routeProvider) {
$routeProvider
.when('/', {
controller: 'HomeController',
templateUrl: 'views/home.html'
}).when('/photos/:id', { controller: 'PhotoController', templateUrl: 'views/photo.html'
}).otherwise({
redirectTo: '/'
});
});
上面的代码意思是'/'的时候map html模板到controller
What did we just do?
- In app.js, we mapped a URL to
PhotoController
andphoto.html
. We added a variable part namedid
to the URL, like this:/photos/:id
. - In PhotoController, we used Angular's
$routeParams
to retrieveid
from the URL by using$routeParams.id
. Notice that we injected both$routeParams
and thephotos
service into thePhotoController
dependency array to make them available to use inside the controller. - Then to fetch an individual photo, we first used the
photos
service to fetch the array of photos from the server, and then used$routeParams.id
to access the specific photo by its index. - As before, any properties attached to
$scope
become available to use in the view. This means in photo.html, we can display the photo'sdetail
using expressions as done before.
Notice that when you click on links, the app doesn't do a full reload. Only the part of the view specified by <div ng-view></div>
changes.
我喜欢这个logo,还想右键保存呢,结果人家是图标字体化的before。。。
.cc-achievement--code-achievement:before {
content: "\e62f";
}
好吧,个人网站上建一个子页做自定义demo去。
延伸阅读 学习资料合集
Angular1.0的更多相关文章
- webpack前端构建angular1.0!!!
webpack前端构建angular1.0 Webpack最近很热,用webapcak构建react,vue,angular2.0的文章很多,但是webpack构建angualr1.0的文章找来找去也 ...
- angular1.0 app
angular 1.0 简单的说一下就是ng启动阶段是 config-->run-->compile/link config阶段是给了ng上下文一个针对constant与provider修 ...
- Angular1.0 在Directive中调用Controller的方法
Controller中定义了$scope.method = function(){} Directive中需要引入$scope http://stackoverflow.com/questions/2 ...
- 要胀爆的Angular1.0
尝试从http请求上遏制缓存: http://blog.csdn.net/u010039979/article/details/54376856 if (!$httpProvider.defaults ...
- Angular1.0路由的Hashbang和HTML5模式
原文答主jupiter http://stackoverflow.com/questions/16677528/location-switching-between-html5-and-hashban ...
- angular1.0 $http jsonp callback
$http.jsonp(sDUrl,{cache:false,jsonpCallbackParam:'callback'}); https://stackoverflow.com/questions/ ...
- angularjs 2.0 快速案例(1)
前言 上一节我们已经把环境给搭建起来了,现在我们通过一个快速案例把angular 2.0 初步了解一下,后续我们会深入每一个细节,这个案例主要是一个[英雄(Hero)]列表的展示,创建,编辑.这个案例 ...
- Webstorm 下的Angular2.0开发之路
人一旦上了年纪,记忆力就变得越来越不好. 最近写了许多的博文,倒不是为了给谁看,而是方便自己来搜索,不然一下子又忘记了. 如果恰巧帮助到了你,也是我的荣幸~~~~~~~~~~~~ 废话不多说,看正题~ ...
- 关于angular1与angular2的应用区别
angular1.0的这些繁杂的api,还有它的执行速度,运行效率,学习曲线,都被人吐槽,最近一直在用ng1,实在很想吐槽. 最近写ng2的项目,写了一些ng2基础的应用(包括angular-cli, ...
随机推荐
- 条带深度 队列深度 NCQ IOPS
http://blog.csdn.net/striping/article/details/17449653 IOPS 即I/O per second,即每秒进行读写(I/O)操作的次数,多用于数据库 ...
- Oracle11g安装完成后给用户解锁
安装时候你可能忘记给普通用户scott解锁,导致安装成功后普通用户无法登录,那该怎么办呢? 先用system用户登录,登录成功之后就可以给其他用户解锁了. 如图: 同理,如果要锁定某一个用户,只需要把 ...
- RedHat7搭建MongoDB
yum安装MongoDB 添加MongoDB源# vi /etc/yum.repos.d/mongodb-org-3.0.repo [mongodb-org-3.0] name=MongoDB Rep ...
- android开发:点击缩略图查看大图
android中点击缩略图查看大图的方法一般有两种,一种是想新浪微博list页面那样,弹出一个窗口显示大图(原activity为背景).另一种就是直接打开一个新的activity显示大图. 1.第一种 ...
- js判断手机端操作系统(Andorid/IOS)
非常实用的js判断手机端操作系统(Andorid/IOS),并自动跳转相应下载界面 androidURL = "http://xxx/xxx.apk"; var browser = ...
- SQL SERVER将某一列字段中的某个值替换为其他的值 分类: MSSQL 2014-11-05 13:11 67人阅读 评论(0) 收藏
SQL SERVER将某一列字段中的某个值替换为其他的值 UPDATE 表名 SET 列名 = REPLACE(列名 ,'贷','袋') SQL SERVER"函数 replace 的参数 ...
- .NET 统一用户管理 -- 单点登录
单点登录 前言 本篇做为.Net 统一用户管理的系列文章的开山之作,主要说一个单点登录是怎么实现的,以及为啥要统一用户管理. 单点登录(Single Sign On),简称为 SSO,是目前比较流行的 ...
- VS2010打不开VS2012 .NET MVC 工程,及打开后部分模块加载不正确的解决办法
转自http://www.xuebuyuan.com/2042634.html 首先,如果sln打开不正确,用(notepad++)打开sln 比如 VS2010的前两行为: Microsoft Vi ...
- C# - linq查询现有的DataTable
可以通过linq对现有的DataTable进行查询,并将结果拷贝至新的DataTable中例如: // Query the SalesOrderHeader table for orders plac ...
- SQL Server2008 附加数据库失败 错误代码5120
由于目录权限不够导致 ,解决办法:将文件所在的文件夹增加everyone 并且赋予完全控制权限问题解决