【问题标题】:Angular don't load the templateAngular 不加载模板
【发布时间】:2015-10-29 14:27:25
【问题描述】:

当我单击链接时,我的 Angular(离子)应用程序不会加载我的模板。因此,如果我单击链接,地址栏中的 url 会发生变化,但模板不会。我留在我的主页上。如果我将 url 自己放在地址栏中并单击 ENTER,我的模板就会加载。

  <ion-side-menus ng-controller="HomeCtrl">
      <ion-side-menu-content>
          <ion-header-bar class="bar-positive">
              <button class="button button-icon ion-navicon" ng-click="toggleLeft()" ng-hide="$exposeAside.active"></button>
              <h1 class="title">Home</h1>
          </ion-header-bar>
          <ion-content>

              <ion-list class="feed">

                  <ion-item class="item">
                      Click <a href="#/friends">Here</a> to go on Friends
                  </ion-item>
                  <ion-item class="item">
                      Click <a href="#/notification">Here</a> to go on Notification
                  </ion-item>

              </ion-list>

          </ion-content>
      </ion-side-menu-content>
      <ion-side-menu expose-aside-when="large" class="panel-menu-left">
          <ion-content>
              <div ng-include src="'menu.html'"></div>
          </ion-content>
      </ion-side-menu>
      <div ng-include src="'tabs.html'"></div>
  </ion-side-menus>

我创建了一个 Plunker:http://plnkr.co/edit/IFHpZF4MOOrRYciLTjAs?p=info

http://run.plnkr.co/owTNxkDqZwhK4QHv/#/

如果您单击通知,则 URL 会更改,但页面不会更改。如果你直接在http://run.plnkr.co/owTNxkDqZwhK4QHv/#/notification 页面加载。

我做错了什么?

【问题讨论】:

  • 控制台有什么错误吗?
  • @ShubhamNigam 不,我主页中的所有其他 js 内容都运行良好(连接、推送菜单等)。只是cordova.js 404(未找到),但这是正常的。
  • 从一个链接路由到另一个链接时出现任何错误
  • 而不是href="#/notification" 尝试使用href ui-sref="notification"
  • @RazvanBalosin 不,我只有这些模块:angular.module('starter', ['ionic', 'http-auth-interceptor', 'ngSanitize']);

标签: angularjs ionic-framework angular-ui-router


【解决方案1】:

这是解决方案。

结构:

www/
    templates/
        menu.html
        hompage.html
        notification.html
        ...
    index.html

app.js:

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
app = angular.module('starter', ['ionic'])

app.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if(window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
  });
});

app.config(function($stateProvider, $urlRouterProvider, $ionicConfigProvider) {
    $ionicConfigProvider.views.transition('none'); //remove this if you want a slide annimation each time you change state
    $stateProvider
        .state('app', {
            url: "/app",
            abstract: true,
            templateUrl: "templates/menu.html"
        })
        .state('app.homepage', {
            url: "/homepage",
                views: {
                'menuContent' :{
                    templateUrl: "templates/homepage.html",
                    controller: "HomeCtrl"
                }
            }
        })
        .state('app.notification', {
            url: "/notification",
                views: {
                'menuContent' :{
                    templateUrl: "templates/notification.html",
                    controller: "NotificationCtrl"
                }
            }
        })
        .state('login', {
            url: "/login",
            templateUrl: "templates/login.html",
            controller: "LoginCtrl"
        })
        .state('app.friends', {
            url: "/friends",
                views: {
                'menuContent' :{
                    templateUrl: "templates/friends.html",
                    controller: "FriendsCtrl"
                }
            }
        })
    ;
  // if none of the above states are matched, use this as the fallback
  $urlRouterProvider.otherwise('/app/homepage');
});

app.controller('AppCtrl', function($scope, $ionicSideMenuDelegate, $state) {
    $scope.toggleLeft = function() {
        $ionicSideMenuDelegate.toggleLeft();
    };
});

app.controller('HomeCtrl', function($scope) {
    console.log("HOME");
});

app.controller('NotificationCtrl', function($scope) {
    console.log("NOTIFICATION");
});

app.controller('FriendsCtrl', function($scope) {
    console.log("FRIENDS");
});

app.controller('LoginCtrl', function($scope, $state) {
    console.log("LOGIN");
    $scope.credentials = {
        username: 'hotgeart',
        password: '123456'
    };
    $scope.submit = function (credentials) {
        alert('Login: '+ $scope.credentials.username +'\nPassword: '+$scope.credentials.password);
        $state.go('app.homepage');
    };
});

index.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
        <title>Test</title>
        <link href="lib/ionic/css/ionic.css" rel="stylesheet">
        <link href="css/style.css" rel="stylesheet">
        <script src="lib/ionic/js/ionic.bundle.js"></script>
        <script src="cordova.js"></script>
        <script src="js/app.js"></script>
    </head>
    <body ng-app="starter" ng-controller="AppCtrl">
        <ion-nav-view></ion-nav-view>
    </body>
</html>

menu.html(父级)

<ion-side-menus enable-menu-with-back-views="true">
    <ion-side-menu-content>
        <ion-nav-bar class="bar-positive">
            <ion-nav-buttons side="left">
                <button class="button button-icon button-clear ion-navicon" menu-toggle="left"></button>
            </ion-nav-buttons>
        </ion-nav-bar>
        <ion-nav-view name="menuContent"></ion-nav-view> <!- THE CONTENT LOAD HERE -->
    </ion-side-menu-content> 
    <ion-side-menu side="left">
        <ion-content>
            <ion-list class="panel-menu-left">
                <ion-item class="item" href="#/login">
                    Login
                </ion-item>
                <ion-item class="item" href="#/app/homepage">
                    Homepage
                </ion-item>
                <ion-item class="item" href="#/app/notification">
                    Notification
                </ion-item>
                <ion-item class="item" href="#/app/friends">
                    Friends
                </ion-item>
            </ion-list>
        </ion-content>
    </ion-side-menu>
    <div class="tabs tabs-icon-only">
        <div class="border"></div>
        <a href="#/app/friends" class="tab-item friends">
            <div class="flag"></div>
            <i class="icon ion-ios-people-outline"></i>
        </a>
        <a href="#/app/notification" class="tab-item notifications">
            <div class="flag"></div>
            <i class="icon ion-ios-bell-outline"></i>
        </a>
        <a href="#/app/friends" class="tab-item visitors">
            <div class="flag"></div>
            <i class="icon ion-ios-eye-outline"></i>
        </a>
    </div>
</ion-side-menus>

模板示例(notification.html、friends.html 和主页相同,只是文字不同)

<ion-view view-title="Home">
    <ion-content>
        <ion-list class="feed">
            <ion-item class="item">
                Click <a href="#/app/friends">Here</a> to go on Friends
            </ion-item>
            <ion-item class="item">
                Click <a href="#/app/notification">Here</a> to go on Notification
            </ion-item>
        </ion-list>
    </ion-content>
</ion-view>

login.html(外部页面测试)

<ion-content class="padding">
    <h1>External page (without menu)</h1>  
    <form role="form" ng-submit="submit(credentials)">
        <div class="alert alert-danger" ng-if="errorMessage">
            {{ errorMessage }}
        </div>
        <div class="list list-inset">
            <label class="item item-input">
                <input type="text" ng-model="credentials.username" placeholder="Username" required>
            </label>
            <label class="item item-input">
                <input type="password" ng-model="credentials.password" placeholder="Password" required>
            </label>
            <br><a href="#/app/homepage">HOMEPAGE</a>
        </div>
        <button class="button button-block button-positive" ng-click="submit(credentials)">Login</button>
    </form>
</ion-content>

希望将来能对某人有所帮助。

【讨论】:

  • 你的错误是什么?你怎么修好它的?请提供有关您的修复/解决方案的更多详细信息。比你能更好地帮助别人。顺便说一句 - 我的回答没有帮助? ;)
  • 我不能添加更多细节,我只是给出正确的方法。这是官方回复:github.com/driftyco/ionic/issues/4200#issuecomment-129966262 没有你的回复没有帮助。
【解决方案2】:

我不知道 ionic,但我认为您的文件夹结构不正确。如果我添加一个简单的内联模板,您的 plunkr 正在工作。例如:

.state('friends', {
     url: '/friends',
     template: '<p>Hello, world!</p>',
     controller: 'FriendsCtrl',
 });

我读到了一些关于 ionic 文件夹结构 herehere 的内容。模板似乎必须在一个特殊的目录中:

来自第一个链接:

项目

  • 挂钩
  • 平台
  • 万维网
    • 模板/

来自第二个链接:

“我们将使用的模板位于我们项目的 www/templates/user.html 中。”

编辑:

如果模板文件夹没问题,我想你已经自己回答了这个问题:你的模板是错误的。正如我已经说过的,我不知道 ionic 但类似 this 的东西可以工作(主页和朋友 - 通知仍然损坏)。从本质上讲,您似乎必须使用 ion-view 之类的东西开始一个模板,如下所示:

<ion-view ng-controller="YourController">
    <ion-content>
...

上面的 plunkr 不是很漂亮,但它说明了这一点并强制执行我之前发表的评论。重构代码以摆脱菜单并将其放在中心位置 - 在我的 plunkr 中,就在 index.html 中。

关于我刚刚找到的模板和结构this here

视图 [模板] 是应用的状态或页面标记的位置 生活。在 Ionic 中,这些文件看起来像这样:

<ion-view title="About">
  <ion-content>
    My super cool content here!
  </ion-content>
</ion-view>

【讨论】:

  • 顺便说一下,我会重构代码以防止所有模板中的菜单代码重复。
  • 我不知道如何在 Plunker 上创建一个文件夹,但在我的本地项目中存在 templates 文件夹(也是这个问题)。
  • 但是使用内联模板它对你有用,对吧? (模板:'

    你好,世界!

    ',)
  • 是的,但问题出在我的模板而不是它们的路径中。
  • 你能用你的浏览器开发者窗口验证你的 html 文件(比如 friends.html)是从服务器获取的吗? (Chrome 中的“网络”标签)
【解决方案3】:
<div class="tabs tabs-icon-only">

 <a ui-sref=“notification" class="tab-item notifications">
  <div class="flag"></div>
  <i class="icon ion-ios-bell-outline"></i>
 </a>
//other items
</div>

查看问题中答案的部分:Angular-ui-router: ui-sref-active and nested states

(为此,$state 应该在视图中可用。)

angular.module('xyz').controller('AbcController', ['$scope', '$state', function($scope, $state) {
   $scope.$state = $state;
}]);)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-25
    • 1970-01-01
    相关资源
    最近更新 更多