On Github yanivefraim / moving-to-components
Yaniv Efraim
Wix.com
** directives can be used for DOM manipulations
Replace:
<div ng-controller="myController"></div>
With:
<my-component></my-component>
//Instead: <div ng-include="my-template.html"></div> //Use: <my-component></my-component> module.component('myComponent', { templateUrl: 'my-template.html'; ... })
Instead of Directives, use:
angular.module('myModule') .directive('myComponent', () => { return { template: `<div>{{myControllerAs.data}}</div>` controller: MyController, controllerAs: 'myControllerAs' bindToController: { input: '=', output: '&' }, scope: {}, link: (scope, element, attrs, ngModelController) => {} } });
angular.module('myModule') .component('myComponent', { template: `<div>{{$ctrl.data}}</div>` controller: MyController, bindings: { input: '=', output: '&' } });
angular.module('myModule') .config(($routeProvider) => { $routeProvider .when('/my-app', { template: `<my-app-component saved-games="$resolve.savedGames"> </my-app-component>`, resolve: { savedGames: function (gameServerApi: GameServerApi) { return gameServerApi.getSavedGames(); } } })
<div ng-controller="MainCtrl as mainCtrl"> <h1>Items</h1> <ul> <li ng-repeat="item in mainCtrl.items"> {{item.text}} <button ng-click="mainCtrl.deleteItem(item)"> Delete </button> </li> </ul> </div>
function MainController() { var ctrl = this; ctrl.items = [{title: 'title 1', text: 'item 1'}, {...}]; ctrl.deleteItem = function(item) { var idx = ctrl.items.indexOf(item); if (idx >= 0) { ctrl.items.splice(idx, 1); } }; }
<div ng-controller="MainCtrl as mainCtrl"> <h1>Items</h1> <!-- <ul> <li ng-repeat="item in mainCtrl.items"> { {item.text}} <button ng-click="mainCtrl.deleteItem(item)"> Delete </button> </li> </ul> --> <item-list items="mainCtrl.items"></item-list> </div>
module.component('itemList', { controller: function() { var ctrl = this; ctrl.deleteItem = function(item) { var idx = ctrl.items.indexOf(item); if (idx >= 0) { ctrl.items.splice(idx, 1); } }; }, bindings: { items: '=' }, templateUrl: 'item-list.html' }; );
(our component is mutating data it doesn't own!)
ctrl.deleteItem = function(item) { var idx = ctrl.items.indexOf(item); if (idx >= 0) { ctrl.items.splice(idx, 1); } };
module.component('itemList', { scope: {}, controller: function() { $ctrl.deleteItem = function(item) { $ctrl.onDelete({item: item}); }; }, controllerAs: 'itemListCtrl', bindings: { items: '=', onDelete: '&' }, templateUrl: 'item-list.html' }; );
<div ng-controller="MainCtrl as mainCtrl"> <h1>Items</h1> <item-list items="mainCtrl.items" on-delete="mainCtrl.onDelete(item)"></item-list> </div>
module.component('itemList', { scope: {}, controller: function() { $ctrl.deleteItem = function(item) { $ctrl.onDelete({item: item}); }; }, controllerAs: 'itemListCtrl', bindings: { items: '=', //input onDelete: '&' //output }, templateUrl: 'item-list.html' }; );
module.component('mainComponent', { template: ` <div> <h1>Items</h1> <item-list items="$ctrl.dataService.items" on-delete="$ctrl.onDelete(item)"></item-list> </div> `, controller: MainComponentController })