angularjs controller code example
Example 1: controller in angularjs projects
var myApp = angular.module('scopeInheritance', []);
myApp.controller('MainController', ['$scope', function($scope) {
$scope.timeOfDay = 'morning';
$scope.name = 'Nikki';
}]);
myApp.controller('ChildController', ['$scope', function($scope) {
$scope.name = 'Mattie';
}]);
myApp.controller('GrandChildController', ['$scope', function($scope) {
$scope.timeOfDay = 'evening';
$scope.name = 'Gingerbread Baby';
}]);
Example 2: angularjs - controllerAs
<div ng-controller="MainController as MC">
…
</div>
Example 3: angularjs - controllerAs
angular.module('myApp', [])
.config(function ($stateProvider) {
$stateProvider
.state('main', {
url: '/',
controller: 'MainController as MC',
templateUrl: '/path/to/template.html'
})
}).
controller('MainController', function () { … });
Example 4: controller in angularjs projects
myApp.controller('ChildController', ['$scope', function($scope) {
$scope.name = 'Mattie';
}]);
myApp.controller('GrandChildController', ['$scope', function($scope) {
$scope.timeOfDay = 'evening';
$scope.name = 'Gingerbread Baby';
}]);