Tomáš Trajan
I ❤️ building useful things! It involves code & design 🛠 Passionate about #Frontend, #typescript, #Angular, #Material #Design ! Obviously the bright Future 🌞
Software Engineer Frontend / Java @mimacom
evolution of Angular JS concepts ($scope, ngRoute, ...)
if we have enough time :)
<div ng-app="myApp" ng-controller="myCtrl">
First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
Full Name: {{firstName + " " + lastName}}
</div>
// myCtrl.js
angular.module('myApp', [])
controller('myCtrl', function($scope) {
$scope.firstName = "John";
$scope.lastName = "Doe";
});
<div ng-app="myApp" ng-controller="parentCtrl">
Parent: <input type="text" ng-model="user.name" />
<div ng-controller="childCtrl">
Child: <input type="text" ng-model="user.name" />
</div>
</div>
// myCtrl.js
angular.module('myApp', [])
.controller('parentCtrl', function($scope) {
$scope.user = {
name: 'John Doe'
}
})
.controller('childCtrl', function($scope) {
});
angular.module('app', []);
.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/someUrl', {
templateUrl: 'some-tempalte.html',
controller: 'SomeController',
resolve: {}
});
// routes.js
angular.module('app', [])
.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/someUrl', {
templateUrl: 'some-tempalte.html',
controller: 'SomeController as ctrl'
});
// someController.js
angular.module('app')
.controller('SomeController', function() {
this.property = 'some value';
});
// some-tempalte.html
<div>
{{ctrl.property}}
</div>
$stateProvider
.state('parent', {
url: '/parent',
templateUrl: 'parent.html'
})
.state('parent.child', {
url: '/child',
templateUrl: 'child.html'
})
.state('parent.anotherchild', {
url: '/anotherchild',
views: {
'': { templateUrl: 'anotherchild.html' },
'sidebar@anotherchild': { // ui-view="sidebar"
template: 'Look I am a sidebar!'
}
}
});
// some app tempalte
<div some-directive prop='value'></div>
// directive
angular.module('app', [])
.directive('someDirective', function () {
return {
scope: {
prop: '@'
},
templateUrl: 'someTemplate.html',
controller: SomeController,
controllerAs: 'ctrl',
bindToController: true // also as of angular 1.4.1 it is possible
// to specify props {} here instead of scope
};
})
.controller('SomeController', function() {
this.prop; // 'value'
});
// directive tempalte: someTemplate.html
<div>{{ctrl.prop}}</div>
angular
.module('app', [])
.directive('someComponent', someComponent);
function someComponent() {
return {
restrict: 'A',
scope: {
// isolated scope, use to pass data from parent, eg: data: '='
},
controller: SomeComponent,
controllerAs: 'ctrl',
bindToController: true,
templateUrl: 'some-component.tpl.html'
};
}
function SomeComponent(SomeService, SomeOtherDependency) {
this.name = 'John Doe';
}
// app.js
angular.module('app', ['ngNewRouter'])
.controller('AppController', ['$router', AppController]);
AppController.$routeConfig([
{path: '/', component: 'home' }
]);
function AppController ($router) {}
// components/home/home.js
angular.module('app.home', [])
.controller('HomeController', [function () {
this.name = 'John Doe';
}]);
// components/home/home.html
<h1>Hello {{home.name}}!</h1>
@Component({
selector: 'some-component',
componentServices: [
SomeService
]
})
@Template({
url: './some-component.html',
directives: [Foreach] // directives used in template
})
class SomeComponent {
constructor() {
this.SomeService = SomeService;
this.name= 'John Doe';
}
doStuff() {
this.SomeService.doStuff();
}
}
// someComponent.js
// directive definition object used to specify component
function someComponent() {
return {
restrict: 'A', // only attribute eg: <div my-component></div>
scope: {
// isolated scope, use to pass data from parent, eg: data: '='
},
controller: SomeComponent, // controller function
controllerAs: 'ctrl', // controller alias in template
bindToController: true, // bind scope props to controller's this
templateUrl: 'some-component.tpl.html' // components template url
};
}
// someComponent.js
function SomeComponent(SomeService) {
var vm = this;
vm.name = 'John Doe';
vm.calculate = calculate;
function calculate(param) {
return SomeService.performCalculation(param);
}
}
// some-component.tpl.html
<div>
{{ctrl.name}}
<button ng-click="ctrl.calculate();">Calculate!</button>
</div>
// some-component.tpl.html
angular
.module('app', ['ui.router'])
.config(config);
function config($stateProvider) {
$stateProvider
.state('app.some', {
url: '/some',
template: '<div some-component></div>',
resolve: {
// ... resolve data, init models
},
});
}
// for multiple routes
$stateProvider
.state('app.admin.users.user', {
url: '/app/admin/users/user/{userId}" ',
template: '<div user-info-component></div>'
})
// ... other states ...
.state('app.user.profile', {
url: '/app/user/{userId}/profile" ',
template: '<div user-info-component></div>'
});
// inside of another component's template... header.tpl.html
<div>
<!-- navigation ... -->
<div user-info-component></div>
<div>
// someComponent.js (ES6)
class SomeComponent {
constructor(SomeService) {
this.someProperty = 'some value accessible in template';
this.SomeService = SomeService;
}
calculate() {
this.SomeService.performCalculation();
}
}
By Tomáš Trajan
How and why to implement Component Pattern in Angular JS 1.X. Lightning overview of evolution of Angular JS concepts, Component Pattern implementation, migration path to Angular 2.0
I ❤️ building useful things! It involves code & design 🛠 Passionate about #Frontend, #typescript, #Angular, #Material #Design ! Obviously the bright Future 🌞