简体   繁体   中英

Routing with angular js, other javascript doesn't work for routed tabs

I am working on a single page app with angularjs. The routing works fine and all and my javascript that's targeted towards elements not in the ng-view works like a charm. However, the javascript that is meant for elements inside the ng-view doesn't work at all.

So for example when I click on my navigation menu the javascript works great and the menu background changes color, however if I navigate to one.html and click on the link, no javascript triggers at all.

index.html

<!DOCTYPE html>
<html>
<head>
    <!-- Load Angular Js -->
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.3/angular-route.min.js"></script>
    <script src="route.js"></script>
</head>
<body ng-app="single-page-app">
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <div ng-controller="cfgController">
                    <nav class="navbar navbar-default">
                        <div class="container-fluid">
                            <div class="navbar-header">
                                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
                                        <span class="sr-only">Toggle navigation</span>
                                        <span class="icon-bar"></span>
                                        <span class="icon-bar"></span>
                                        <span class="icon-bar"></span>
                                    </button>
                                    <a class="navbar-brand" href="#">Navigation</a>
                            </div>
                            <div id="navbar" class="navbar-collapse collapse">
                                    <ul class="nav navbar-nav">
                                        <li><a href="#/1">One</a></li>
                                        <li><a href="#/2">Two</a></li>
                                        <li><a href="#/3">Three</a></li>
                                        <li><a href="#/4">Four</a></li>
                                    </ul>
                            </div>
                        </div>
                    </nav>
            </div>
        </div>
    </div>
    <div ng-view>

    </div>
</div>

<!-- JavaScript placed at bottom for faster page loading times -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script>
    $('.navbar-nav>li>a').click(function() {
        $('.navbar-nav>li').css('background', '#fcfcfc');
        $(this).parent().css('background', '#337ab7');
    });

    $('.test').click(function() {
        alert('Working!');
    })
</script>

route.js

var app=angular.module('single-page-app',['ngRoute']);
app.config(function($routeProvider){
    $routeProvider
        .when('/1',{
            templateUrl: 'one.html'
        })
        .when('/2',{
            templateUrl: 'two.html'
        })
        .when('/3',{
            templateUrl: 'three.html'
        })
        .when('/4',{
          templateUrl: 'four.html'
        })
});
app.controller('cfgController',function($scope){

});

one.html

<div class="row">
    <div class="col-md-12">
        <a href="#" class="test">Test Js</a>
    </div>
</div>

Your ng-view is outside of any controller.

You have to define controllers for your routes (partial views) like this:

var app=angular.module('single-page-app',['ngRoute']);
app.config(function($routeProvider){
    $routeProvider
        .when('/1',{
            templateUrl: 'one.html',
            controller: 'oneCtrl'
        })
        .when('/2',{
            templateUrl: 'two.html',
           controller: 'twoCtrl'
        })

        .when('/3',{
            templateUrl: 'three.html',
            controller: 'threeCtrl'
        })
        .when('/4',{
          templateUrl: 'four.html',
          controller: 'fourCtrl'
        })
});
app.controller('cfgController',function($scope){

});
app.controller('oneCtrl',function($scope){

});
app.controller('twoCtrl',function($scope){

});
app.controller('threeCtrl',function($scope){

});

After that you can do your javascript for some view in corresponding controller. Your routing logic will assure that every partial view that you inject in your ng-view is using controller defined for that view (or template).

You can use

$('body').on('click', '.navbar-nav>li>a', function() {
    $('.navbar-nav>li').css('background', '#fcfcfc');
    $(this).parent().css('background', '#337ab7');    
)};

But it's a bad way to make angular app. Normally you wouldn't create event handlers outside angular app scope. You should use ng-click at least. And the better way is creating a directive.

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