简体   繁体   中英

Wraping angular directives,controllers and routes into functions

So i still new to angular and java script but i read it was good practical to wrap the everything into functions.

This is what i have done. in comment is the previous version

app.js

//var app = angular.module('app',['ngRoute','ngResource','routes']);


(function(angular) {
    angular.module("app.directives", []);
    angular.module("app.controllers", []);
    angular.module("app", ['ngRoute','ngResource','routes','app.directives','app.controllers']);
}(angular));

directives.js

/*
 app.directive('footer', function () {
 return {

 replace: true,
 templateUrl: "/template/main_footer.html",
 controller: ['$scope', '$filter', function ($scope, $filter) {

 }]
 }
 });
 */

(function(angular) {

    var footer = function () {
        return {

            replace: true,
            templateUrl: "/template/main_footer.html",
            controller: ['$scope', '$filter', function ($scope, $filter) {


            }]

        };
    };

    footer.$inject = [];
    angular.module("app.directives").controller("footer", footer);

});

controller.js

/*app.controller('HeaderController',function($scope, $location) {

    $scope.isActive = function (viewLocation) {
        var active = (viewLocation === $location.path());
        return active;
    };

});*/

(function(angular) {

    var HeaderController = function($scope,$location){

        $scope.isActive = function(viewLocation){
            var active = (viewLocation === $location.path());
            return active;
        };

    }

    HeaderController.$inject = ['$scope','$location'];
    angular.module("app.controllers").controller("HeaderController", HeaderController);

})

and how should i proceed for routes.js

angular.module('routes', []).config(function ($routeProvider) {
    $routeProvider
        .when('/', {
            templateUrl: 'pages/home.html'
        })
        .when('/second', {
            templateUrl: 'pages/second.html'
        })
        .when('/third', {
            templateUrl: 'pages/third.html'
        })
        .when('/123', {
            templateUrl: 'pages/123.html'
        })

        .otherwise({
            redirectTo: '/'
        });

});

index.html

<!DOCTYPE html>
<html ng-app="app">
<head>


    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/custom.css">
    <script type="text/javascript" src="https://code.angularjs.org/1.4.4/angular.js"></script>
    <script type="text/javascript" src="https://code.angularjs.org/1.4.4/angular-resource.js"></script>
    <script type="text/javascript" src="https://code.angularjs.org/1.4.4/angular-route.js"></script>


    <script type="text/javascript" src="js/routes.js"></script>
    <script type="text/javascript" src="js/app.js"></script>
    <script type="text/javascript" src="js/controllers.js"></script>
    <script type="text/javascript" src="js/directives.js"></script>

</head>
<body>

<div ng-view></div>


</body>
</html>

But it does not work. And i find it hard to find what went wrong because i don't get a error with the development tools on google chrome

Update

Now i do call the function at the end. But i still can't see the footer. I also added the footer.html just in case there would something i forgot there. directives.js

(function(angular) {

    var footer = function () {
        return {

            replace: true,
            templateUrl: "/template/main_footer.html",
            controller: ['$scope', '$filter', function ($scope, $filter) {

            }]

        };
    };
    footer.$inject = [];
    angular.module("app.directives").controller("footer", footer);

}(angular));

home.html

<div>
    <div>
        <h3>This is the homepage</h3>
    </div>
    <div footer></div> 
</div>

That is because you're missing the function invocation in some of those wrappings. For example, controller.js needs to be:

(function(angular) {

    var HeaderController = function($scope,$location){

        $scope.isActive = function(viewLocation){
            var active = (viewLocation === $location.path());
            return active;
        };

    }

    HeaderController.$inject = ['$scope','$location'];
    angular.module("app.controllers").controller("HeaderController", HeaderController);

})(angular); // CALL FUNCTION

Note in the last line I added (angular); to actually call the function.

In the 'directives.js' and 'controller.js' files, you forgot (angular) at the end, like in 'app.js'.

If you write a function like this:

function f() {
    // do stuff...
}

it's never going to run unless you call it somewhere: f(); . Now, if you want to wrap a bit of code in a function, you still want it to run immediately as if it had not been wrapped. So what you do is you call the wrapping function immediately like this:

(function f() {
    // do stuff...
})();

or like this (same thing):

(function f() {
    // do stuff...
}());

In that second way of writing things, the outermost parentheses are useless for the program but they help the reader see that the function will be immediately run (or "evaluated", or "called").

You can also pass anything that lives outside of the function into the function, for example angular :

(function f(angular) {
    // do stuff...
}(angular));

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