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

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.

主站蜘蛛池模板: 武宁县| 龙南县| 宁海县| 姚安县| 阿瓦提县| 宁明县| 张掖市| 安远县| 巴彦县| 荥阳市| 通城县| 龙游县| 泰州市| 杭锦后旗| 阳江市| 东兴市| 余江县| 彰化市| 平江县| 兰州市| 长寿区| 东丰县| 长白| 武平县| 西城区| 曲阳县| 东兰县| 和龙市| 玛曲县| 乌拉特前旗| 沈丘县| 侯马市| 闽清县| 安福县| 五大连池市| 合江县| 富宁县| 汝南县| 鸡东县| 尚志市| 清新县|