How to access Angular service in view expression?

Angular documentation recommends using Angular services Angular expressions:

Instead, use services such as $ window and $ location in functions called from expressions. Such services provide mock access to global variables.

- https://docs.angularjs.org/guide/expression#context

However, these services are not displayed in scope by default, for example.

{{$location || 'Undefined'}}

Will produce "Undefined".

If I want to access a service $locationfrom a view, do I need to enter it into the scope?

/* some controller */ function ($scope, $location) {
    $scope.$location = $location;
}
+4
source share
1 answer

, , . , URL , , , - .

VIEW

<p>{{ CurrentUrl }}</p>

(function(app){

    app.controller("myController",["$scope","$location",function($scope,$location){

        $scope.CurrentUrl = $location.$$path; // or whatever is most useful to display

    }]);

})(myApp);

, IOC Angular "" .

"$ location" , , . "$ window" .

+1

Source: https://habr.com/ru/post/1568519/


All Articles