angularjsng-viewangularjs-rootscope

How to make a generic close button for angular ui-views?


I need a function where I can use $window.history.back() to be called from every view regardless of what controller is controlling that page.

.run(['$rootScope', '$state', 'CommonUserModel', 'InitialiseService','$window', function($rootScope, $state, $window, commonUserModel, initialiseService) {

    $rootScope.link = function(){
        $window.history.back();
    };

So I have this function put in the app module. Injected window object because it complained about it. But now it also complains that it "Cannot read property 'back' of undefined".

I am calling this function with $rootScope from other controllers as I read through StackOverflow. I had to inject $rootScope to other controllers like this.

homeViewModule.controller("simDetailsController", [ '$rootScope','$scope', 'ModalDialogService', 'CommonTagModel', '$location','$window',
                                        function($scope, modalDialogService, commonTagModel, $location, $window,$rootScope) { 
self.link = function(){
   $rootScope.link();};

Can you give me an advice? Keep in mind that I am pretty newbie on AngularJS I still don't get this messy, complex framework.


Solution

  • The order in which you have injected $window & $rootScope in the parameters list does not match the string array list.
    You have mentioned '$window' as the 5th element in the string array, while, it is the 3rd element in the parameters list.
    Also, in your code, '$rootScope' is the 1st element in the string array, while, it is the 6th element in the parameters list.

    Replace the first lines in both of your code snippets with these:

    .run(['$rootScope', '$state', 'CommonUserModel', 'InitialiseService','$window', function($rootScope, $state, commonUserModel, initialiseService, $window) {

    homeViewModule.controller("simDetailsController", [ '$rootScope','$scope', 'ModalDialogService', 'CommonTagModel', '$location','$window', function($rootScope, $scope, modalDialogService, commonTagModel, $location, $window) {