Kamil Augustynowicz / @AugKamil
Jak zadeklarować dyrektywę
Scope i przekazywanie parametrów
Gdzie wrzucić logikę
Gotowe dyrektywy w AngularJS
angular.module "movieApp", []
.directive "movieTitle", ->
restrict: String
priority: Number
terminal: Boolean
template: String or Template Function:
(tElement, tAttrs) ->
...
templateUrl: String
replace: Boolean or String
scope: Boolean or Object
transclude: Boolean
controller: String or
(scope, element, attrs, transclude, otherInjectables) ->
...
controllerAs: String
require: String
link: (scope, iElement, iAttrs) ->
...
compile:
(tElement, tAttrs, transclude) ->
...
angular.module 'movieApp',[]
.directive 'movieTitle', ->
restrict: "E"
...
angular.module 'movieApp',[]
.directive 'movieTitle', ->
restrict: "A"
...
angular.module 'movieApp',[]
.directive 'movieTitle', ->
restrict: "C"
...
angular.module 'movieApp',[]
.directive 'movieTitle', ->
restrict: "M"
...
movie_app = angular.module 'movieApp',[]
movie_module.directive 'movieReview', ->
restrict: "E"
scope: {}
template:
''+
'Twoja recenzja: {{review}}'
movie_app = angular.module 'movieApp',[]
movie_app.controller "AppCtrl", ($scope) ->
$scope.showInfo = (title) ->
alert "Dodaj do ulubionych filmów: " + title
movie_app.directive 'movieTitle', ->
restrict: "E"
scope:
title: "@"
favoriteMovie: "&"
template:
'
movie_app = angular.module 'movieApp',[]
movie_app.controller "AppCtrl", ($scope) ->
$scope.rates = ["Tragedia", "Słaby", "Średni", "Dobry", "Rewelacyjny"]
movie_app.directive 'movieRating', ->
restrict: "E"
scope:
rate: "="
rates: "="
template:
'
movie_app = angular.module 'movieApp',[]
movie_app.directive 'movieRating', ->
...
link: (scope, element, attrs, ctrls) ->
element.bind 'mouseover', ->
element.css 'color', 'red'
element.bind 'mouseleave', ->
element.css 'color', 'black'
scope.$watch "rate", ((newValue, oldValue) ->
if !angular.equals(newValue, oldValue)
if newValue != "Rewelacyjny"
alert "Czy jesteś pewien tej oceny?"
), true
...
movie_app = angular.module 'movieApp',[]
movie_app.directive 'movieHeading', ->
require: ["^title", "^director"]
link: (scope, element, attrs, ctrls) ->
ctrls[0].showTitle("Powrót do Przyszłości")
ctrls[1].showDirector("Robert Zemeckis")
...
{{movie.title}}
ng-href
ng-src
ng-disabled
ng-checked
ng-readonly
ng-selected
ng-class
ng-style
ng-view
ng-form
ng-submit
ng-select
ng-cloak
ng-init
Aby połączyć z logiką
Jako opakowanie dla plugin-ów jQuery
Modyfikowanie elementów DOM
DRY
Kamil Augustynowicz / @AugKamil