To pass data from the controller
to view
in AngularJS we use the following:
Angular Scope
var x = angular.module("myApp",[]);
x.controller('myController',function($scope) {
$scope.var1 = "valor1";
$scope.varN = "valorN";
});
Receiving only the values contained within the scope $scope
.
In the official documentation of AngularJS
(appendix at the end) referencing the service $window
(global JavaScript object), they use the following configuration:
Angular + Javascript(Window) scope
var myApp = angular.module('myApp', []);
x.controller('myController', ['$scope', '$window', function($scope, $window) {
$scope.var1 = "var1";
$scope.varN = "varN";
$scope.var1JS = $window.var1JS;
$scope.var2JS = $window.var2JS;
}]);
Receiving values from the scopes $scope
and $window
.
Why do you use an array with the two scopes ( $scope
, $window
) in addition to the default function in the parameters of the controller
, if it is possible that it works only by passing the parameter $window
in an additional way?, like this:
let myApp = angular.module("myApp",[]);
myApp.controller("myController",function($scope,$window) {
$scope.var1 = "valor1";
$scope.varN = "valorN";
$scope.var1Js=$window.var1Js;
$scope.var2Js=$window.var2Js;
});
Assuming from the first method that if the controller recognizes the service $scope
by default, you could do it with $window
.
Addendum: https://docs.angularjs.org/api/ng/service/ $window
An array is used as a parameter in the controllers so that Angular can identify which services to inject when compressing/obfuscating the javascript code.
If we compress the first one, angular will not be able to identify the services specified in the function's constructor and will throw an error:
While if we compress/obfuscate the following code, angular knows which services to inject by means of the service names specified in the array:
Where: parameter
r = $scope
parameterb = $window