javascript - AngularJS 中的动态路由问题

标签 javascript angularjs routes

我在网站的某个部分中使用动态路由,并使用 ng-repeat 显示项目列表。当有人单击特定项目时,其各自的内容将显示在新 View 上。

这是我的 ng-repeat 代码部分:

<input type="text" placeholder="SEARCH" ng-model="search" />
        <div id="track-list" ng-repeat="track in tracks | filter:search">
        <a ng-href="#/radio/{{$index}}"><h3 class="track-list-title">{{track.title}}</h3></a>
        <p class="track-list-date">{{track.date | date}}</p>
</div>

这是主索引文件:

    <!DOCTYPE html>
<html ng-app="mainApp">
<head lang="en">
    <meta charset="utf-8">
    <title>Playlife Project</title>
    <!-- styles -->
    <link rel="stylesheet" href="css/bootstrap.css" />
    <link rel="stylesheet" href="css/style.css" />
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
    <!-- scripts -->
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js"></script>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.28//angular-route.min.js"></script>
    <script src="http://d2v52k3cl9vedd.cloudfront.net/plangular/3.1.0/plangular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular-animate.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>

<header>
    <div id="logo"><a href="#/"><img src="img/logo.png" class="logo img-responsive" /></a></div>
    <div id="nav" class="menu">
        <ul>
            <li class="menu-item"><a href="#/podcast">Podcast</a></li>
            <li class="menu-item"><a href="#/record">Record Label</a></li>
            <!--<li class="menu-item"><a href="#/live">Live Act</a></li>-->
            <li class="menu-item"><a href="#/about">About</a></li>
            <li class="menu-item"><a href="#/contact">Get In Touch</a></li>
            <li class="menu-item"><a href="#">Shop</a></li>
            <li class="mobile-menu-item"><i class="fa fa-2x fa-bars"></i></li>
        </ul>
    </div>

    <div id="drawer" class="mobile-menu">
        <ul>
            <li><a href="#/podcast">Podcast</a></li>
            <li><a href="#/record">Record Label</a></li>
            <li><a href="#/live">Live Act</a></li>
            <li><a href="#/contact">Get In Touch</a></li>
        </ul>
    </div>

</header>

<div ng-controller="homeController" class="{{pageClass}}" ng-view ></div>

<footer>
    <div id="subscribe">
        <form name="newsletter">
            <input class="inp" type="email" name="email" id="email" placeholder="Be a part of the journey">
            <input class="button" type="button" name="button" id="button" value="GET ON BOARD">
        </form>
    </div>
    <div id="social">
        <ul class="social-icons">
            <li><a href="" target="_blank"><img src="img/facebook.png" /></a></li>
            <li><a href="" target="_blank"><img src="img/soundcloud.png" /></a></li>
            <li><a href="" target="_blank"><img src="img/twitter.png" /></a></li>
            <li><a href="" target="_blank"><img src="img/youtube.png" /></a></li>
            <li><a href="" target="_blank"><img src="img/instagram.png" /></a></li>
        </ul>
    </div>
</footer>

<script type="text/javascript" src="script/main.js"></script>
<script type="text/javascript" src="script/script.js"></script>
</body>
</html>

这是主应用程序:

  var mainApp = angular.module("mainApp", ['ngRoute', 'ngAnimate', 'plangular']);

/* page routing */

mainApp.config(function($routeProvider) {
    $routeProvider
        .when('/', {
            templateUrl: 'views/home.html',
            controller: 'homeController'
        })
        .when('/podcast', {
            templateUrl: 'views/podcast.html',
            controller: 'podcastController'
        })
        .when('/radio/:id', {
            templateUrl: 'views/radio.html',
            controller: 'radioController'
        })
        .when('/record', {
            templateUrl: 'views/record.html',
            controller: 'recordController'
        })
        .when('/about', {
            templateUrl: 'views/about.html',
            controller: 'aboutController'
        })
        /*.when('/live', {
            templateUrl: 'views/live.html',
            controller: 'liveController'
        })*/
        .when('/contact', {
            templateUrl: 'views/contact.html',
            controller: 'contactController'
        })
        .otherwise({
            redirectTo: '/'
        });
})

/* player */

.config(function(plangularConfigProvider){
    plangularConfigProvider.clientId = 'f39fd2f8db018982adef87c3ecd63e7a';
})

/* home controller */

.controller('homeController', function($scope, $timeout) {
    $scope.pageClass = 'page page-home';

    var INTERVAL = 5000, slides =[{head:"PLAY LIFE PROJECT", btn:'Listen to Playlife Podcast #002', link:'#', src:"./img/slide-1.jpg"},
        {head:"SUBMIT YOUR TRACKS TO GET FEATURED ON PLAY LIFE RECORDS", btn:'Submit your track', link:'#', src:"./img/slide-2.jpg"},
        {head:"PLAY LIVE CONNECT IS COMING NEAR YOU", btn:'Check Events', link:'#', src:"./img/slide-3.jpg"},
        {head:"LISTEN TO PLAY LIVE PODCAST", btn:'Listen', link:'#', src:"./img/slide-4.jpg"}];

    function setCurrentSlideIndex(index) {
        $scope.currentIndex = index;
    }

    function isCurrentSlideIndex(index) {
        return $scope.currentIndex === index;
    }

    function nextSlide() {
        $scope.currentIndex = ($scope.currentIndex < $scope.slides.length - 1) ? ++$scope.currentIndex : 0;
        $timeout(nextSlide, INTERVAL);
    }

    function loadSlides() {
        $timeout(nextSlide, INTERVAL);
    }

    $scope.slides = slides;
    $scope.currentIndex = 0;
    $scope.setCurrentSlideIndex = setCurrentSlideIndex;
    $scope.isCurrentSlideIndex = isCurrentSlideIndex;

    loadSlides();

})

.controller('podcastController', ['$scope', function($scope) {
    $scope.pageClass = 'page page-podcast';
    $scope.tracks =[
        {title:'Group Therapy 150 with Above & Beyond and Maor Levi', date: new Date(), link:'#'},
        {title:'Group Therapy 151 with Above & Beyond and Martin Garrix', date: new Date(), link:'#'},
        {title:'Group Therapy 152 with Above & Beyond and Grum', date: new Date(), link:'#'},
        {title:'Group Therapy 153 with Above & Beyond and Andrew Bayer', date: new Date(), link:'#'},
        {title:'Group Therapy 154 with Above & Beyond and Ilan Bluestone', date: new Date(), link:'#'},
        {title:'Group Therapy 155 with Above & Beyond and Armin Van Buuren', date: new Date(), link:'#'},
        {title:'Group Therapy 156 with Above & Beyond and Hardwell', date: new Date(), link:'#'}
    ]
}])

.controller('radioController', ['$scope', '$routeParams', function($scope, $routeParams) {
    $scope.pageClass = 'page';
    $scope.track=$scope.tracks[ $routeParams.id]
}])

.controller('recordController', function($scope) {
    $scope.pageClass = 'page page-record';
})

.controller('aboutController', function($scope) {
    $scope.pageClass = 'page-about';
})

/*
mainApp.controller('liveController', function($scope) {
    $scope.pageClass = 'page-live';
});
*/

.controller('contactController', function($scope) {
    $scope.pageClass = 'page page-contact';
});

当我点击这些项目时,它们会正确显示,它们会重定向到这个新 View ,但不会显示标题,我看到的只是 {{track.title}}

在检查 Google Chrome 的控制台时,我收到此错误:

TypeError: Cannot read property '4' of undefined
    at new <anonymous> (http://localhost:63342/New%20website/script/main.js:97:31)
    at d (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js:34:265)
    at Object.instantiate (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js:34:394)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js:66:112
    at link (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.28//angular-route.min.js:7:248)
    at J (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js:53:345)
    at f (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js:46:399)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js:46:67
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js:47:303
    at u (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js:51:28) <div ng-controller="homeController" class="{{pageClass}} ng-scope" ng-view="">

最佳答案

问题是 $scope.tracks 是在 podcastController 中定义的,而您在 radioController 中访问它,而 radioController 不知道 podcastController。您可以将 radioController 嵌套在 podcastController 中,也可以将 $scope.tracks 的初始化移至 homeController。

尝试移动

  $scope.tracks =[
      {title:'Group Therapy 150 with Above & Beyond and Maor Levi', date: new Date(), link:'#'},
      {title:'Group Therapy 151 with Above & Beyond and Martin Garrix', date: new Date(), link:'#'},
      {title:'Group Therapy 152 with Above & Beyond and Grum', date: new Date(), link:'#'},
      {title:'Group Therapy 153 with Above & Beyond and Andrew Bayer', date: new Date(), link:'#'},
      {title:'Group Therapy 154 with Above & Beyond and Ilan Bluestone', date: new Date(), link:'#'},
      {title:'Group Therapy 155 with Above & Beyond and Armin Van Buuren', date: new Date(), link:'#'},
      {title:'Group Therapy 156 with Above & Beyond and Hardwell', date: new Date(), link:'#'}
  ]

进入 homeController 并更新

<div ng-controller="homeController" class="{{pageClass}}" ng-view ></div>

<div ng-controller="homeController" class="{{pageClass}}">
    <div class="{{pageClass}}" ng-view ></div>
</div>

关于javascript - AngularJS 中的动态路由问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34121100/

相关文章:

php - 如何最好地实现数据库驱动的动态 url/ Controller 名称?

javascript - 授权回调错误

javascript - 在对象数组中创建唯一列表和数组项计数

javascript - 在 AngularUI 路由器中, Unresolved promise 会泄漏吗?

c# - Selenium sendKeys 不输入所有文本

javascript - 只允许在文本框中输入范围内的数字

json - Golang JSON 路由配置

javascript - 如何禁用 dijit.form.Select 中的单个选项?

Javascript:多层条件,使用 "return false"停止以后的条件检查

javascript - jquery中变量的未定义属性