Angular.js 學(xué)習(xí)筆記

This is the Angular.js study note of Shaping up with Angular.js.

Shaping up with Angular.js is a set of videos teaching angular.js.

1 Mr-why’s Sex Store

1.1 Ramp Up

  • Directive

    • Directive is a maker on HTML tag that tells Angular to run or reference some Javascript code.
  • Modules

    • Where we write pieces of our Angular application.
    • Makes our code more maintainable, testable, and readable.
    • Where we design dependencies for our app.
    • <html ng-app=“store”>
      var app = angular.module(’store’, []);
  • Controllers

    • Where we add application behavior
  • Expressions

    • Allow you to insert dynamic values into your HTML.
    • I am {{4 + 6}} —> I am 10
      {{“hello” + “you”}} —> hello you

1.2 Index HTML Setup

  • Controller
    • Controllers are where we define our apps behavior by defining functions and values.
    • Notice that controller is attached to (inside) of our app.
  • An example
controller

1.3 Built-in Directives

  • How to control a button to show / hide?
    • define new property to gem as: canBuy: false
    • add ng-show to the controlled button: ng-show="sc.product.canBuy"
    • In the same way, you can use ng-hide to get the same outcome.
  • How to add multi-products?
    • for gem, write it as a set, and rename product as products (unnecessary).
    • when use gem, two ways: (1) use it as a set: sc.products[0].price; (2) automatic generate with ng-repeat: ng-repeat=“product in sc.products"
Built-in Directives

2 Built-in Directives

2.1 Filters and a New Directive

  • Formatting with filters
    • {{ data | filter:options }}
    • Some eg:
      • date: {{‘1388123412323’ | date:’MM/dd/yyyy @ h:mma’}} —> 12/27/2013 @ 12:50 AM
      • uppercase & lowercase: {{’octagon gem’ | uppercase}} —> OCTAGON GEM
      • limitTo: {{‘My Product’ | limitTo: 8}} —> My Produ
        <li ng-repeat=“product in store.products | limitTo:3”> —> only first 3 products in products set will show
      • orderBy: <li ng-repeat=“product in store.products | orderBy:’-price’”> —> order by price
  • Using ng-src for images
    • <img ng-src=“{{product.images[0].full}}">
    • never forget the two braces

2.2 Tabs Inside Out

  • Use ng-click & ng-show to control tabs
  • Use ng-init to init an active tab
  • Use ng-class to set active style for active tab-pill
panel-dirty version
  • But this code is dirty, to keep the logic of angular outside of HTML, use controller
panel - clean version

3 Forms

3.1 Forms and Models

  • ng-model
    • binds the form element value to the property.

3.2 Accepting Submissions

  • ng-submit
    • allows us to call a function when the form is submitted.

3.3 Validation

  • no validate

    • used in form tag to turn off default HTML validation
  • required

    • used in tag to mark required fields
  • <div>reviewFrom is {{reviewForm.$valid}}</div>

    • used to print forms validity
    • this should be used with ng-submit to check submit.
  • angular class for input style:

    • Source before typing email:
      <input name=“author” … class=“ng-pristine ng-invalid">
    • Source after typing, with invalid email
      <input name=“author” … class=“ng-dirty ng-invalid">
    • Source after typing, with valid email
      <input name=“author” … class=“ng-dirty ng-valid">
    • Note that color must be indicated before using.
form

4 Custom Directives

4.1 Directives

  • Using ng-include for Templates

    • ng-include is expecting a variable with the name of the file to include. To pass the name directly as a string, use single quotes ‘ ‘
    • e.g. ng-include=“name_price.html”
  • custom directive

    • Directives are like: <book-title></book-title>
    • Directives allow you to write HTML that expresses the behavior of your application. AWSOME!
  • How to build a directive

    • 1-define tag, such as <product-title></product-title>
    • 2-use app.directive to control the directive in JavaScript. Note that dash in HTML translates to camelCase in JavaScript.
directive

4.2 Directive Controllers

  • Move the controller in the directive
controller in directive

5 Services

5.1 Dependencies

  • How to organize the application modules?
    • app.js is the top level module attached via ng-app
    • products.js is all the functionality for products and only products.
    • The specific codes refer to the attachment code.
  • The decency is defined with:
    var app = angular.module('store', ['store-products']);
    where the store-products in the ‘[ ]’ is dependency.

5.2 Services

  • Some useful services:
    • Fetching JSON data from a web service with $http
    • Logging messages to the JavaScript console with $log
    • Filtering an array with $filter
  • $http service
    • Use as function:
      $http({method: ‘GET’, url: ‘/products.json’});
    • Use one if the shortcut methods:
      $http.get(‘/products.json’, { apiKey: ‘myApiKey’ });
    • Both return a Promise object with .success() and .error()
services

保持署名鏈接 MR-WHY (http://mr-why.com) 及原文鏈接 http://mr-why.com/post/category/frontend/angularjs 的情況下歡迎轉(zhuǎn)載 :) 萧福。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末拉鹃,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子鲫忍,更是在濱河造成了極大的恐慌膏燕,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,123評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件悟民,死亡現(xiàn)場離奇詭異坝辫,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)射亏,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,031評論 2 384
  • 文/潘曉璐 我一進(jìn)店門近忙,熙熙樓的掌柜王于貴愁眉苦臉地迎上來宦赠,“玉大人整胃,你說我怎么就攤上這事状共∠耄” “怎么了丐谋?”我有些...
    開封第一講書人閱讀 156,723評論 0 345
  • 文/不壞的土叔 我叫張陵住闯,是天一觀的道長踏烙。 經(jīng)常有香客問我踢涌,道長兼蜈,這世上最難降的妖魔是什么攘残? 我笑而不...
    開封第一講書人閱讀 56,357評論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮饭尝,結(jié)果婚禮上肯腕,老公的妹妹穿的比我還像新娘。我一直安慰自己钥平,他們只是感情好实撒,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,412評論 5 384
  • 文/花漫 我一把揭開白布姊途。 她就那樣靜靜地躺著,像睡著了一般知态。 火紅的嫁衣襯著肌膚如雪捷兰。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,760評論 1 289
  • 那天负敏,我揣著相機(jī)與錄音贡茅,去河邊找鬼。 笑死其做,一個胖子當(dāng)著我的面吹牛顶考,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播妖泄,決...
    沈念sama閱讀 38,904評論 3 405
  • 文/蒼蘭香墨 我猛地睜開眼驹沿,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了蹈胡?” 一聲冷哼從身側(cè)響起渊季,我...
    開封第一講書人閱讀 37,672評論 0 266
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎罚渐,沒想到半個月后却汉,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,118評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡荷并,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,456評論 2 325
  • 正文 我和宋清朗相戀三年合砂,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片源织。...
    茶點(diǎn)故事閱讀 38,599評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡既穆,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出雀鹃,到底是詐尸還是另有隱情,我是刑警寧澤励两,帶...
    沈念sama閱讀 34,264評論 4 328
  • 正文 年R本政府宣布黎茎,位于F島的核電站,受9級特大地震影響当悔,放射性物質(zhì)發(fā)生泄漏傅瞻。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,857評論 3 312
  • 文/蒙蒙 一盲憎、第九天 我趴在偏房一處隱蔽的房頂上張望嗅骄。 院中可真熱鬧,春花似錦饼疙、人聲如沸溺森。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,731評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽屏积。三九已至医窿,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間炊林,已是汗流浹背姥卢。 一陣腳步聲響...
    開封第一講書人閱讀 31,956評論 1 264
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留渣聚,地道東北人独榴。 一個月前我還...
    沈念sama閱讀 46,286評論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像奕枝,于是被迫代替她去往敵國和親棺榔。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,465評論 2 348

推薦閱讀更多精彩內(nèi)容