Upgrade to Pro — share decks privately, control downloads, hide ads and more …

一些 Angular 的最佳实践

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.

一些 Angular 的最佳实践

Avatar for Bin Xin

Bin Xin

July 01, 2015
Tweet

Other Decks in Programming

Transcript

  1. ֵአ ControllerAs դ๊ $scope <script> angular.module('app', []) .controller('FooCtrl', function ()

    { this.foo = 'this is foo' }) </script> <div ng-app="app" ng-controller="FooCtrl as ctrl"> {{ ctrl.foo }} </div> 2015.7 2
  2. ࣁ Directive Ӥֵአ module.directive('fooBar', function () { return { scope:

    {}, template: '{{ ctrl.foo }}', controllerAs: 'ctrl', controller: function () { this.foo = 'this is foobar directive' } } }) 2015.7 5
  3. Directive ጱᳯ᷌ module.directive('fooBar', function () { return { scope: {

    data: '=' }, template: '{{ ctrl.foo }} {{ ctrl.data.xx }}', controllerAs: 'ctrl', controller: function ($scope) { this.foo = 'this is foobar directive' // handle data var self = this $scope.$watch('data', function (data) { self.data = data }) } } }) 2015.7 6
  4. ཛྷࣘဳف // directives/foo.js return function ($scope, $timeout) { return {

    scope: {} template: 'xxx' link: function () {} } } // module.js module.directive('foo', ['$scope', '$timeout', foo]) 2015.7 17
  5. // directives/foo.js return ['$scope', '$timeout', function ($scope, $timeout) { return

    { scope: {} template: 'xxx' link: function () {} } }] // module.js module.directive('foo', foo) 2015.7 18