Yaniv Efraim
Wix.com
** directives can be used for DOM manipulations
Replace:
With:
//Instead:
//Use:
module.component('myComponent', {
templateUrl: 'my-template.html';
...
})
Instead of Directives, use:
angular.module('myModule')
.directive('myComponent', () => {
return {
template: `{{myControllerAs.data}}`
controller: MyController,
controllerAs: 'myControllerAs'
bindToController: {
input: '=',
output: '&'
},
scope: {},
link: (scope, element, attrs, ngModelController) => {}
}
});
angular.module('myModule')
.component('myComponent', {
template: `{{$ctrl.data}}`
controller: MyController,
bindings: {
input: '=',
output: '&'
}
});
angular.module('myModule')
.config(($routeProvider) => {
$routeProvider
.when('/my-app', {
template: `
`,
resolve: {
savedGames: function (gameServerApi: GameServerApi) {
return gameServerApi.getSavedGames();
}
}
})
Items
-
{{item.text}}
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);
}
};
}
Items
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'
};
);
Items
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: `
Items
`,
controller: MainComponentController
})