AngularJSã¨RequireJSãåããã¦ä½¿ã£ã¦ã¿ã
ã¢ã¸ã¥ã¼ã«åä½ã§ãã¡ã¤ã«ã«åãããããªã¼ã¨æã£ãã®ã§ã
AngularJSæ¬å®¶ã®ãã¥ã¼ããªã¢ã«ã®phonecatAppãé¡æã«ãã¦ã
AngularJS + RequireJS | StarterSquadãåèã«ã¡ãã£ã¨ã¢ã¬ã³ã¸ã
çµæã®ã½ã¼ã¹ã¯こちら
åä½ãã¹ãã®å®è¡ã¨çµåããã½ã¼ã¹ãproduction.htmlã§è¦ããã¨ããã¾ã§ç¢ºèªã
ã¢ããªã®å®ç¾©
phonecatApp.controllersã«ã¯PhoneListCtrlã¨PhoneDetailCtrlãå
¥ã£ã¦ãã
phonecatApp.filtersã«ã¯checkmarkã
phonecatApp.servicesã«ã¯Phoneãããããå
¥ã£ã¦ãã
app/assets/js/app.js
define([ 'angular', 'angularRoute', 'angularResource', './services/Phone', './filters/checkmark', './controllers/PhoneListCtrl', './controllers/PhoneDetailCtrl' ], function(angular) { 'use strict'; var phonecatApp = angular.module('phonecatApp', [ 'ngRoute', 'phonecatApp.controllers', 'phonecatApp.filters', 'phonecatApp.services' ]); phonecatApp.config([ '$routeProvider', function($routeProvider) { $routeProvider. when('/phones', { templateUrl: 'assets/js/templates/phone-list.html', controller: 'PhoneListCtrl' }). when('/phones/:phoneId', { templateUrl: 'assets/js/templates/phone-detail.html', controller: 'PhoneDetailCtrl' }). otherwise({ redirectTo: '/phones' }); } ]); return phonecatApp; });
ã¢ã¸ã¥ã¼ã«å®ç¾©ï¼ä¾ï¼ã³ã³ããã¼ã©ï¼
ä¾åé¢ä¿ã§èªã¿è¾¼ãã§ããmoduleï¼app/assets/js/controllers/module.jsï¼ããã¤ã³ãã«ãªãã
app/assets/js/controllers/PhoneListCtrl.js
define([ './module', '../services/Phone' ], function(controllers) { 'use strict'; return controllers.controller('PhoneListCtrl', [ '$scope', 'Phone', function($scope, Phone) { $scope.phones = Phone.query(); $scope.orderProp = 'age'; } ]); });
moduleã§phonecatApp.controllersãå®ç¾©ãã¦ã³ã³ããã¼ã©ãç»é²ãããã
servicesã¨ããä¼¼ããããªæãã§åã¾ã¨ã¾ãã§moduleä½ã£ã¦ããã«ç»é²ãããã
app/assets/js/controllers/module.js
define([ 'angular', ], function(ng) { 'use strict'; return ng.module('phonecatApp.controllers', []); });
ã¢ããªã®èµ·å
ng-appãã£ã¬ã¯ãã£ãã ã¨éã«åããªãã®ã§ãangular.bootstrapã使ã£ã¦æåã§phonecatAppãå®è¡ããã
app/assets/js/bootstrap.js
require([ 'angular', 'app' ], function(ng) { 'use strict'; ng.bootstrap(document, ['phonecatApp']); });
ææ³
defineãrequireã¨angular.module両æ¹ã§ä¾åå®ç¾©ããå¿
è¦ãããã®ã§ãã¡ãã£ã¨å¾®å¦ã