고요빛물결
앱개발

angularjs 1.4.5의 분석

1. 브라우저가 Angular.js 파일을 읽고 실행한다.

2. DOM Content Loaded Event 트리거를 기다린다.

[code]

// check if document is already loaded

    if (document.readyState === 'complete') {

      setTimeout(trigger);

    } else {

      this.on('DOMContentLoaded', trigger); // works for modern browsers and IE9

      // we can not use jqLite since we are not done loading and jQuery could be loaded later.

      // jshint -W064

      JQLite(window).on('load', trigger); // fallback to window.onload for others

      // jshint +W064

    } 

[/code]

3. 트리거가 오면 Angularinit을 실행한다. 

[code]

  jqLite(document).ready(function() {

    angularInit(document, bootstrap);

  }); 

[/code]

4. anuglarinit에서 HTML구조를 파싱해서 ng-app을 찾는 것 같습니다. ng-, data-ng-, ng:, x-ng- 네개의 프리픽스를 찾아보네요. 

[code]

var ngAttrPrefixes = ['ng-', 'data-ng-', 'ng:', 'x-ng-']; 

------

function angularInit(element, bootstrap) {

  var appElement,

      module,

      config = {};

 

  // The element `element` has priority over any other element

  forEach(ngAttrPrefixes, function(prefix) {

    var name = prefix + 'app';

 

    if (!appElement && element.hasAttribute && element.hasAttribute(name)) {

      appElement = element;

      module = element.getAttribute(name);

    }

  });

  forEach(ngAttrPrefixes, function(prefix) {

    var name = prefix + 'app';

    var candidate;

 

    if (!appElement && (candidate = element.querySelector('[' + name.replace(':', '\\:') + ']'))) {

      appElement = candidate;

      module = candidate.getAttribute(name);

    }

  });

  if (appElement) {

    config.strictDi = getNgAttribute(appElement, "strict-di") !== null;

    bootstrap(appElement, module ? [module] : [], config);

  }

} 

[/code]

5. 위에서 html에 ng-app가 있으면 appElement = 'html'이 되고, body에 있으면 body를 가지고 bootstrap으로 넘어 가네요 

 

Automatic Initialization

concepts-startup.png

Angular initializes automatically upon DOMContentLoaded event or when theangular.js script is evaluated if at that time document.readyState is set to'complete'. At this point Angular looks for the ng-app directive which designates your application root. If the ng-app directive is found then Angular will:

  • load the module associated with the directive.
  • create the application injector
  • compile the DOM treating the ng-app directive as the root of the compilation. This allows you to tell it to treat only a portion of the DOM as an Angular application.
<!doctype html>
<html ng-app="optionalModuleName">
  <body>
    I can add: {{ 1+2 }}.
    <script src="angular.js"></script>
  </body>
</html>

As a best practice, consider adding an ng-strict-di directive on the same element as ng-app:

<!doctype html>
<html ng-app="optionalModuleName" ng-strict-di>
  <body>
    I can add: {{ 1+2 }}.
    <script src="angular.js"></script>
  </body>
</html>

This will ensure that all services in your application are properly annotated. See the dependency injection strict mode docs for more. 

댓글 2개

근데 이제 2.0해야 하는것 아닌가요? ㅠㅜ
저 고민되요.
http://www.hanbit.co.kr/network/view.html?bi_id=1981 지금 배우신다고 하면 2.0으로 하시는 것도 좋죠.. 앞으로의 대세는 모바일쪽이니 mobile first로 가시는 것도...
댓글을 작성하시려면 로그인이 필요합니다.

소모임

23,243건
+
분류 제목 글쓴이 날짜 조회
컴퓨터 15-09-12 조회 3,622
고양이 15-09-12 조회 1,544
고양이
[고양이]
15-09-12 조회 1,615
코딩야화TV
[코딩야화TV]
15-09-12 조회 1,515
해외사용자
[해외사용자]
15-09-12 조회 1,287
코딩야화TV
[코딩야화TV]
15-09-12 조회 1,977
프리사자모 15-09-12 조회 2,345
낚시 15-09-12 조회 2,258
프리사자모 15-09-12 조회 1,759
해외사용자 15-09-12 조회 1,432
소지구 15-09-12 조회 4,396
제로보드만세 15-09-12 조회 2,218
해외사용자
[해외사용자]
15-09-12 조회 1,232
음악/악기
[음악/악기]
15-09-12 조회 770
노래방 15-09-12 조회 728
앱개발 15-09-12 조회 2,555
해외사용자 15-09-12 조회 1,060
해외사용자 15-09-12 조회 1,471
소모임 신청 15-09-11 조회 2,003
모형취미 15-09-11 조회 1,994
컴퓨터 15-09-11 조회 7,776
컴퓨터 15-09-11 조회 6,975
음악/악기
[음악/악기]
15-09-11 조회 1,093
노래방 15-09-11 조회 1,318
맥사용자 15-09-11 조회 4,211
고양이 15-09-11 조회 1,900
컴퓨터 15-09-11 조회 1.2만
개발자애환 15-09-11 조회 1,383
소모임 신청 15-09-11 조회 1,772
고양이 15-09-11 조회 1,731