简体   繁体   中英

Angular js use $windows from directive code

OK, just when I think I understand AngularJS I get zapped.

I have an application that uses a number of different google maps. I want the user to click on a marker and then have the system so to a new screen with information relating to what was clicked.

Everything is working well up to a point. I get the click event and then get ready to go to the appropriate screen. My code at that point looks like:

$window.location.href =  "#/" + ScreenName + "/" + Parameter ;

At this point I get the error:

ReferenceError: Can't find variable: $window

Which searching tells me I need to inject $window

I have tried a bunch of different ways to do this injection, but this is also where my personnal knowledge base fails me.

I think I need to have my app.js file look like this:

.config([
    '$routeProvider', 
    function($routeProvider) 
        {
        $routeProvider.
        when("/customer/:cust_gid", {templateUrl: "views/div_Cust.html", controller: "customerController"}).
        when("/location/:locn_gid", {templateUrl: "views/div_Locn.html",controller: "locationController"}).
        otherwise({redirectTo:  '/utilities'});
        }])
.config(function ($windowProvider) {
   var $window = $windowProvider.$get();
   console.log($window);
 });

This does nothing for me. I need to know If I am close and just don't have the syntax right or is there something else missing.

Do I need an include file in my index.html file to load $windows ?

Can someone give me a kindergarten level answer to this question.

Appreciate Stan

$windows is a globally service which is included in angularjs. You don't need an extra library reference for it.

To use it, just inject is as any other service in the controller which should use it

app.controller('locationController', function($scope, $window)..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM