Angular

기억하기

  • 뷰 (템플릿) 내에서는 $scope를 명시해주지 않아도 됨

    • 컨트롤러에서 $scope.carname; 정의해두고 뷰에서 '<h1>{carname}}</h1>' 이렇게 사용가능

  • scope는 뷰와 컨트롤러 모두 접근 가능

  • 앵귤러의 MVC 란

    • View = HTML.

    • Model (=scope) = data available for the current view

    • Controller = JavaScript function that makes/changes/removes/controls the data.

바인딩

바인딩이란 -> synchronization between 모델 and 뷰

  • {{}}와 ng-bind 는 동일

  • ng-init은 사실 잘 안씀, 아래 예시용

모듈

모듈은 애플리케이션 컨트롤러의 컨테이너입니다. 컨트롤러는 항상 모듈에 속합니다.

Directive

모듈과 컨트롤러 각각 파일에

  • 앵귤러는 또는 시작 부분에서 로드하는 것이 좋다.

Directives

  • ng-app

    • 앵귤러 앱의 root element를 정의

    • 애플리케이션을 초기화

  • ng-init

    • 앱 데이터를 초기값 설정

  • ng-model

    • HTML input, select, textarea 의 값들을 앱 데이터에 바인딩

네이밍

  • 생성시 카멜 케이스 사용 (e,g. w3TestDirective)

  • HTML 에서 사용할때는 - 포함 (e,g. w3-test-directive)

호출

아래 4가지 방식 모두 동일 기능

  • E: Element name

  • A: Attribute

  • C: Class

  • M: Comment

호출 제한

  • E for Element name

  • A for Attribute

  • C for Class

  • M for Comment

컨트롤러

앵귤러는 Model 과 View 를 즉시 동기화 시켜주기에, 컨트롤러는 모델 데이터만 집중 (어차피 뷰에 영향주니)

ng-model 모델로 바인딩 할경우 (firstName, lastName)

다른 예제

Scope

  • 뷰와 컨트롤러의 바인딩된 부분

    • 그리고, 뷰와 컨트롤러 양쪽에서 사용 가능

  • 스코프는 properties 와 methods 를 이용한 수 있는 객체이다.

Root Scope

Filter

  • | ----> 파이프 기호로 사용

  • uppercase 예시

  • currency Format a number to a currency format.

  • date Format a date to a specified format.

  • filter Select a subset of items from an array.

  • json Format an object to a JSON string.

  • limitTo Limits an array/string, into a specified number of elements/characters.

  • lowercase Format a string to lower case.

  • number Format a number to a string.

  • orderBy Orders an array by an expression.

  • uppercase Format a string to upper case.

orderBy 예시

커스텀 필터

서비스

$location

$http

$timeout (= window.setTimeout)

$interval (= window.setInterval)

커스텀 서비스

커스 사용하려면, 컨트롤러 정의할때 넘겨주기

커스텀 필터에서 커스텀 서비스 사용시

DOM

ng-disabled

ng-show

ng-hide

Events

  • ng-blur

  • ng-change

  • ng-click

  • ng-copy

  • ng-cut

  • ng-dblclick

  • ng-focus

  • ng-keydown

  • ng-keypress

  • ng-keyup

  • ng-mousedown

  • ng-mouseenter

  • ng-mouseleave

  • ng-mousemove

  • ng-mouseover

  • ng-mouseup

  • ng-paste

Validation

아래 모두 true/false 중 하나 값이 지정됨

  • $untouched The field has not been touched yet

  • $touched The field has been touched

  • $pristine The field has not been modified yet

  • $dirty The field has been modified

  • $invalid The field content is not valid

  • $valid The field content is valid

$valid 예제

$touched $invalid 예제

input 에 e 가 들어가있는지 확인 예제

유저 이름과 이메일이 둘중 하나라도 올바르지 않으면 disabled 시키기

나중에 보기

여기부터 공부하기

https://www.w3schools.com/angular/angular_http.asp

잡것

Repeat 반복문

ng-model

  • 아래 과 $scope.name 바인딩

  • HTML input, select, textarea 의 값들을 앱 데이터에 바인딩

양방향 바인딩

ng-model은 아래들을 자동 생성

  • ng-empty

  • ng-not-empty

  • ng-touched

  • ng-untouched

  • ng-valid

  • ng-invalid

  • ng-dirty

  • ng-pending

  • ng-pristine

ng-show

  • 앵귤러는 ng-model에 정의한 변수가 존재하지 않으면 새로 생성해줌.

  • 유저 input 검사기

$routeProvider

뷰 마다 컨트롤러 지정하기

HTML 직접 쓰려면, templateUrl 말고 template 사용

완전체 실습

  • https://www.w3schools.com/angular/angular_application.asp

Last updated