javascript - UI-router 未路由到新模板

标签 javascript angularjs html visual-studio-cordova

我正在努力找出为什么我的模板未加载。我使用 $state.go 函数路由到我的新状态。该函数运行但模板似乎未加载。

app.js:

angular.module('starter', ['ionic', 'starter.controllers', 'ionic.contrib.ui.tinderCards', 'ui.router'])
    .config(function ($stateProvider, $urlRouterProvider) {

    $stateProvider
        .state('favorites', {
            url: '/favoritesDisplay',
            templateUrl: 'templates/favorites.html',
            controller: 'FavoritesCtrl'
        })
        .state('login', {
            url: '/login',
            templateUrl: 'templates/login.html',
            controller: 'LoginCtrl'
        })
        .state('FindEvents', {
            url: '/findEvents',
            templateUrl: 'index.html',
            controller: 'EventsCtrl'
        })

        // if none of the above states are matched, use this as the fallback
        $urlRouterProvider.otherwise('/favoritesDisplay');
    });

Controller .js:

angular.module('starter.controllers', [])
    .directive('noScroll', function () {
        return {
            restrict: 'A',
            link: function ($scope, $element, $attr) {
                $element.on('touchmove', function (e) {
                    e.preventDefault();
                });
            }
        }
    })
    .controller('EventsCtrl', function ($scope, $state) {
         var cardTypes = [
            { image: './images/event1.jpeg', title: 'New Apple Release' },
            { image: './images/event2.jpeg', title: 'Digital Conference' },
            { image: './images/event3.jpg', title: 'Skyline Sessions' },
            { image: './images/event4.jpg', title: 'Secret Rooftop Party' },
            { image: './images/event5.jpeg', title: 'Smoking Lights' },
            { image: './images/event6.jpg', title: 'Antibes Color Run' },
            { image: './images/event7.jpg', title: 'Tomorrowland' },
            { image: './images/event8.jpeg', title: 'Steve Aoki Lighting Up Town' },
            { image: './images/event9.jpeg', title: 'Nice Yacht Party' },
            { image: './images/event10.jpg', title: 'Night Pool Party' },
        ];

        $scope.cards = [];

        $scope.addCard = function () {
            for (var p = 0; p < 10; p++) {
                var newCard = cardTypes[p];
                newCard.id = Math.random();
                $scope.cards.push(angular.extend({}, newCard));
            }
        }

        $scope.addCard();

        $scope.cardDestroyed = function (index) {
            $scope.cards.splice(index, 1);
        };

        $scope.cardSwipedLeft = function (index) {
            console.log('Left swipe');
        }

        $scope.cardSwipedRight = function (index) {
            console.log('Right swipe');
        }

        $scope.cardDestroyed = function (index) {
            $scope.cards.splice(index, 1);
            console.log('Card removed');
        }

        //Transitioning between states
        $scope.Favorites = function () {
            $state.go('favorites');
        }
    });

index.html:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"> 
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
    <link rel="stylesheet" type="text/css" href="css/index.css">
    <title>SlidingTransitionwithAPI</title>

    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <script src="lib/ionic/js/ionic.bundle.js"> </script>
    <script src="cordova.js"></script>

    <script src="lib/angular-ui-router/release/angular-ui-router.js"></script>
    <script src="lib/angular-ui-router/release/angular-ui-router.min.js"></script>

    <script src="js/app.js"></script>
    <script src="js/controllers.js"></script>

    <script src="lib/ionic-ion-swipe-cards/ionic.swipecards.js"></script>

    <script src="lib/collide/collide.js"></script>
    <script src="lib/ionic-ion-tinder-cards/ionic.tdcards.js"></script>


</head>
<body ng-app="starter" no-scroll>
    <ion-pane ng-controller="EventsCtrl">
        <div class="bar bar-header bar-dark">
            <button class="button button-clear button-icon icon ion-navicon"></button>
            <div class="h1 title" font="6" color="white">Event Finder</div>
            <button class="button button-clear" ng-click="Favorites()">
                <i class="icon ion-heart"></i>
            </button>
        </div>

        <td-cards>
            <td-card id="td-card" ng-repeat="card in cards" on-destroy="cardDestroyed($index)"
                         on-swipe-left="cardSwipedLeft($index)" on-swipe-right="cardSwipedRight($index)"
                         on-partial-swipe="cardPartialSwipe(amt)">
                <div class="title">
                    {{card.title}}
                </div>
                <div class="image">
                    <div class="no-text overlayBox"><div class="noBox boxed">Trash</div></div>
                    <img ng-src="{{card.image}}">
                    <div class="yes-text overlayBox"><div class="yesBox boxed" id="centerMe">Save</div></div>
                </div>
            </td-card>
        </td-cards>
    </ion-pane>
</body>

任何帮助将不胜感激。

最佳答案

您需要 ui-view 标签来显示模板的内容。例如:

<body ng-app="starter" no-scroll>
    <div ui-view>
</body>

将正文的当前内容移动到另一个模板(例如“templates/events.html”),然后将其添加到状态配置中。

如果您在状态配置中指定 Controller ,则不需要使用 ng-controller 指令(就像使用 EventsCtrl 一样)。

关于javascript - UI-router 未路由到新模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38409851/

相关文章:

javascript - Window.Open() 错误地转义了?

javascript - 正确使用 ng-submit

javascript - 如何段落绘制到 Canvas 上的文本

javascript - 根据另一个输入值隐藏输入

javascript - ng-dirty 没有按预期工作

html - 使用 YQL 加载多个 url 时取回 url

javascript - 饼图( Highcharts )内的标签没有距离技巧

javascript - 为什么 keyup 事件没有正确触发?

javascript - amcharts4 处理对数值轴上零值的正确方法

javascript - 如何在 angularjs View 页面中求和价格?