Szybka pomoc

AngularJS Cheat Sheet

Systematyczny przewodnik po frameworku AngularJS, obejmujący podstawowe pojęcia, takie jak dyrektywy, powiązanie danych oraz wstrzykiwanie zależności (dependency injection), aby pomóc programistom szybko opanować podstawy tworzenia aplikacji w Angularze.

· Opublikowano

AngularJS to framework JavaScript.

Lista (ng-repeat)

    <ul ng-controller="MyListCtrl">
      <li ng-repeat="phone in phones">
        {{phone.name}}
      </li>
    </ul>

Model (ng-model)

    <select ng-model="orderProp">
      <option value="name">Alphabetical</option>
      <option value="age">Newest</option>
    </select>

Definiowanie modułu

    App = angular.module('myApp', []);

    App.controller('MyListCtrl', function ($scope) {
      $scope.phones = [ ... ];
    });

Zapobieganie minifikacji kontrolera

    App.controller('Name', [
      '$scope',
      '$http',
      function ($scope, $http) {
      }
    ]);

    a.c 'name', [
      '$scope'
      '$http'
      ($scope, $http) ->
    ]

Usługi (Services)

    App.service('NameService', function($http){
      return {
        get: function(){
          return $http.get(url);
        }
      }
    });

Wywołanie z parametrami w kontrolerze wykorzystuje obietnice (promises) do zwracania danych z serwera.

    App.controller('controllerName',
    function(NameService){
      NameService.get()
      .then(function(){})
    })

Dyrektywy (Directives)

    App.directive('name', function(){
      return {
        template: '<h1>Hello</h1>'
      }
    });

W HTML użycie <name></name> wyrenderuje Twój szablon <h1>Hello</h1>

HTTP

  App.controller('PhoneListCtrl', function ($scope, $http) {
    $http.get('/data.json').success(function (data) {
      $scope.phones = data;
    })
  });

Źródła:

Powiązane tematy