Injecting a Built-in Provider into a Controller
Listing 3.1 shows a very basic example of using dependency injection to inject functionality from the $scope
and $window
service into an AngularJS controller. The AngularJS app defined in Listing 3.1 is very simple. On line 2 it defines a basic controller and uses dependency injection to inject the $scope
and $window
service, and then in lines 3 and 4 it uses the $window
service to display an alert pop-up with a message stored in the $scope
.
Listing 3.2 shows the HTML page that uses the myApp module defined in Listing 3.1 and implements the view for controllerA
. Figure 3.1 shows the resulting web page and alert message.
Get Learning AngularJS now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.