Controller defined in the Global Scope:
function HelloController( $scope ) {
$scope.name = "Guest";
}
Controller defined using “.controller” method (Recommended way)
var helloApp = angular.module( "helloApp", [] );
helloApp.controller( "HelloController", function($scope){
$scope.name = "Guest";
} );
Controller instantiation without array notation
var helloApp = angular.module( "helloApp", [] );
helloApp.controller( "HelloController", function($scope){
$scope.name = "Guest";
} );
Controller instantiation with array notation (Recommended way)
var helloApp = angular.module( "helloApp", [] );
helloApp.controller( "HelloController", ['$scope', function($scope){
$scope.name = "Guest";
}]);
beforeEach(
inject(
function( $rootScope, $controller ){
scopeMock = $rootScope.$new();
$controller( 'CompanyCtrl', {$scope: scopeMock} );
}
)
);
The preferred way of injecting the dependencies is by passing the dependency to the constructor function rather than using one of the following other ways. In this way, the responsibility of creating the dependency object lies with other objects or function. This is straight forward for those who have worked with one or more dependency injection framework in the past. However, this could be useful information for the beginners. Following are other ways of doing dependency injection in Angular:
[adsenseyu1]
Last updated: 3rd May, 2024 Have you ever wondered why some machine learning models perform…
Last updated: 2nd May, 2024 The success of machine learning models often depends on the…
When working on a machine learning project, one of the key challenges faced by data…
Last updated: 1st May, 2024 The bias-variance trade-off is a fundamental concept in machine learning…
Last updated: 1st May, 2024 As a data scientist, understanding the nuances of various cost…
Last updated: 1st May, 2024 In this post, you will learn the concepts related to…
View Comments
One thing to keep in mind... The script tag's "async" and "defer" attributes can help with the loading issues.