AngularJS

  • AngularJS is rendering <br> as text not as a newline
    I used ng-bind-html
{{foo.something.replace("\n","<br>")}}
<br ng-bind-html="foo.something">
</br>
  • kebab-case
    kebab case: e.g. "The-quick-brown-fox-jumps-over-the-lazy-dog" ng-app
    CamelCase
    "TheQuickBrownFoxJumpsOverTheLazyDog"

1 . The ng-app attribute represents an Angular directive, named ngApp (Angular uses kebab-case for its custom attributes and camelCase for the corresponding directives which implement them). This directive is used to flag the HTML element that Angular should consider to be the root element of our application. This gives application developers the freedom to tell Angular if the entire HTML page or only a portion of it should be treated as the AngularJS application.

Angular expressions are JavaScript-like code snippets that are evaluated by Angular in the context of the current model scope, rather than within the scope of the global context (window
).
e.g. Nothing here {{'yet' + '!'}}

  • The injector that will be used for dependency injection is created.
    用于依赖注入的注入器将被创建
  • The injector will then create the root scope that will become the context for the model of our application.
  • Angular will then "compile" the DOM starting at the ngApp root element, processing any directives and bindings found along the way.
    injector被创建,然后$rootScope被创建,根元素和绑定的数据就可以被处理了。
Screen Shot 2016-12-08 at 1.22.59 PM.png

For Angular applications, we encourage the use of the Model-View-Controller (MVC) design pattern to decouple the code and separate concerns.
ng-app把view(html)和controller规划到一个app,然后ng-controller表明view绑定的controller是哪一个,从而得到绑定的数据;
data modal 就是$scope定义的变量;比如:

$scope.phones = [ 
{ name: 'Nexus S', 
snippet: 'Fast just got faster with Nexus S.' }, 
{ name: 'Motorola XOOM™ with Wi-Fi', 
snippet: 'The Next, Next Generation tablet.' }, 
{ name: 'MOTOROLA XOOM™', 
snippet: 'The Next, Next Generation tablet.' } ];

Although the controller is not yet doing very much, it plays a crucial role. By providing context for our data model, the controller allows us to establish data-binding between the model and the view.
controller给data modal提供了容器或者说上下文,允许我们在modal和view之间进行数据绑定。
看吧,就像我说的,连接用的:
A scope can be seen as the glue which allows the template, model, and controller to work together.

2 . components

Remember that (since components are also directives) the name of the component is in camelCase(e.g. myAwesomeComponent), but we will use kebab-case(e.g. my-awesome-component) when referring to it in our HTML.
定义的时候用的是camelCase这种方式,使用的时候用的是kebab-case这种方式。
Let's see an example:

angular. module('myApp')
. component('greetUser', 
{ 
template: 'Hello, {{$ctrl.user}}!', 
controller: function GreetUserController() { this.user = 'world'; } 
});

Now, every time we include <greet-user></greet-user> in our view, Angular will expand it into a DOM sub-tree constructed using the provided template and managed by an instance of the specified controller.

命名注意事项A note on file naming:
It is a good practice to distinguish different types of entities by suffix. In this tutorial, we are using the .component suffix for components, so the definition of a someComponent component would be in a file named some-component.component.js

  • $state.go()强制刷新页面方法:
    定义的时候:
.state('test', {       url: '/test',
     cache:'false', 
     params:{bio:null},
     templateUrl: 'templates/test.html', 
      controller: 'testCtrl'     }) 
.state('test', {       
     url: '/test',  
     cache:'false', 
     params:{bio:null},
     views:{
   'main':{
        templateUrl: 'templates/test.html', 
      controller: 'testCtrl'  
}   }) 
3 . phone list的结构:
app/
│
└─phone-list/
│       │─ phone-list.component.js
│       │─ phone-list.component.spec.js
│       │─ phone-list.module.js
│       └─ phone-list.template.html  
└───app.css
 │       
└───app.module.js
│       
└───index.html
4 . dependency injection (DI)

相对路径: (the URL is relative to our index.html file).
get data from Json file:

$http.get('phones/phones.json')
.then(function(response) { self.phones = response.data; });

$http makes an HTTP GET request to our web server, asking for phones.json(the URL is relative to our index.html file). The server responds by providing the data in the JSON file. (The response might just as well have been dynamically generated by a backend server.
The $http service returns a promise object, which has a then() method.

We call this method to handle the <u>asynchronous response 异步响应</u> and assign the phone data to the controller, as a property called phones
.

5 . JAVA
src/
 └─resource/
 │     │
       └─login.properties
 └─com/
    │
    └─makanet/
        │                   
        └─project/
             │                   
             └─util/
                 │─ Constants.java
                 │─ Initializer.java
                 └─ HttpRequests.java

Constants.java如下定义:

public class Constants{
   public static String COMPANY = "Makanet";
   public static String SERVER = "";
}

如果想要在Initializer.java用Constants.java中的常量,需要在Initializer.java引入这个文件

import com.makanet.project.util.Constants;
public class Initializer{
   public void init(){
     //直接用Constants.COMAPYNAME;
//也可以初始化Constants中的值:
Constants.SERVER = "http://localhost:8000";
   }
}
6 . components
7 . components
8 . components

/***********************华丽丽分割线************************/


  • AngularJS中transclude用法:
    Transclude - 在Angular的指令中,大家会看到有一个这样的一个配置属性,这个单词在英文字典里面也查询不到真实的意思,所以就用英文来标示它吧。如果你深入的使用angular的话,你就花很大一部分时间来创建自定义指令,那么就不可避免的要深入理解transclude。简单的讲,transclude主要完成以下工作,取出自定义指令中的内容(就是写在指令里面的子元素),以正确的作用域解析它,然后再放回指令模板中标记的位置(通常是ng-transclude标记的地方),虽然使用内建的ngTransclude对于基本的transclude操作已经足够简单,但是在文档中对这个transclude的解释还是有存在很多疑惑,比如说:
    在fiddle中查看例子:http://jsfiddle.net/ospatil/A969Z/157/

    我用到的是,在做directive时,如果想把数据放到directive的某个元素里,就需要用到transclude了。

  • ng-bind和ng-model的不同

<div ui-view="editBio"></div>
.state('editProfile',{
url:'/editProfile',
views:{
'main':{
templateUrl:'templates/ views/editProfile.html',
controller:'EditProfileController'
}
}
}) .state('editBio',{
url:'/editBio',
params:{bio:null
},
views:{
'main':{
templateUrl:'templates/ views/editBio.html',
controller:'EditBioController'
}
}
})

- common(文件夹)
  - js(文件夹)
    - routes.js
    - controllers(文件夹)
    - constants.js
  - templates(文件夹)
  - css(文件夹)
  - fonts(文件夹)
  - images(文件夹)
  - libs(文件夹)
  - index.js

----
constants.js:

angular.module('com.companyName.projectName.constants',[])
.constant('CONSTANTS',{
MENU_COLORS:['menu-color-white','menu-color-blue','menu-color-red','menu-color-green']
});

CSS:

.menu-color-white {
color: white;
}
.menu-color-blue {
color: blue;
}
.menu-color-red {
color: red;
}
.menu-color-green {
color: green;
}

Controller.js:

angular.module('com.companyName.projectName.constants')
.controller('ControllerName',function($scope, $rootScope, CONSTANTS){
$scope.backgroundColor = CONSTANTS.MENU_COLORS[0];
});

----
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,835评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,598评论 1 295
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,569评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,159评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,533评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,710评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,923评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,674评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,421评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,622评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,115评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,428评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,114评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,097评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,875评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,753评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,649评论 2 271

推荐阅读更多精彩内容