官术网_书友最值得收藏!

Controllers match components

Components are the backbone of Angular and Ionic applications; you will see that almost everything is a component. You can compare controllers in Angular 1 to components in Angular 4. In controllers we used to define most of our code's logical part. We used to register the controllers with our main module. Now, in a similar way, we define our code in components in Angular 4 and if required we can export that Component class:

    // Angular & Ionic 1

angular.module('wedding.controllers', [])
.controller('LoginCtrl',
function($scope, CategoryService) {
// controller function and DI of CategoryService
}
);

// Angular 4 & Ionic 32

import {Component} from '@angular/core';
import {NavController} from 'ionic-angular';

@Component({
templateUrl: 'build/pages/catalog/categories.html'
})
export class CategoryPage {
// DI of NavController for navigation
constructor(private navCtrl: NavController) {
this.nav = navCtrl;
}
}

We have dependency injection in Angular 1 as controller function arguments, while in Angular 4 we pass it inside the constructor function. Many other things such as the IIFE syntax, which we have to define in Angular 1 for keeping out controller code of the global namespace, now are not required in Angular 4 because ES 2015 modules handle name spacing for us. Also, as you can see, we have exported the CategoryPage class, we can now import it wherever this module is required.
Another major change is that $scope is replaced by the this keyword. $scope had many performance issues and already in Angular 1 developers have reduced the usage of $scope.

主站蜘蛛池模板: 武川县| 祁东县| 黄梅县| 濉溪县| 峨眉山市| 辉南县| 黄冈市| 布拖县| 伊宁市| 东阳市| 南城县| 呈贡县| 白城市| 古田县| 广东省| 玛曲县| 松滋市| 深圳市| 濮阳市| 仪陇县| 彭阳县| 玛沁县| 英山县| 赤壁市| 福鼎市| 瑞金市| 读书| 石泉县| 大埔县| 绥滨县| 凤翔县| 武安市| 江川县| 普陀区| 壤塘县| 西乌珠穆沁旗| 大城县| 沙雅县| 敦化市| 疏附县| 宁陵县|