【问题标题】:Routing in angularjs materialdesignangularjs材料设计中的路由
【发布时间】:2015-04-25 13:59:54
【问题描述】:

我试图弄清楚如何在 angularjs 材料设计中的页面之间进行路由。

在此示例中,我想在单击侧边栏链接时更改页面 http://codepen.io/kyleledbetter/pen/gbQOaV

Script.js

// script.js

// create the module and name it scotchApp
    // also include ngRoute for all our routing needs
var starterApp = angular.module('starterApp', ['ngRoute']);

// configure our routes
starterApp.config(function($routeProvider) {
    $routeProvider

        // route for the home page
        .when('/', {
            templateUrl : 'pages/home.html',
            controller  : 'mainController'
        })

        // route for the about page
        .when('/about', {
            templateUrl : 'pages/about.html',
            controller  : 'aboutController'
        })

        // route for the contact page
        .when('/contact', {
            templateUrl : 'pages/contact.html',
            controller  : 'contactController'
        });
});

// create the controller and inject Angular's $scope
starterApp.controller('mainController', function($scope) {
    // create a message to display in our view
    $scope.message = 'Everyone come and see how good I look!';
});

starterApp.controller('aboutController', function($scope) {
    $scope.message = 'Look! I am an about page.';
});

starterApp.controller('contactController', function($scope) {
    $scope.message = 'Contact us! JK. This is just a demo.';
});

about.html

<div class="jumbotron text-center">
    <h1>About Page</h1>

    <p>{{ message }}</p>
</div>

【问题讨论】:

  • 能不能把你的html页面写成about.html等所有的
  • 我没有完全理解你的问题,请详细解释
  • 当您单击侧面菜单中的链接时,我正在尝试显示另一个页面。我之前在 angularJs 中使用 script.js 完成了此操作。现在我正在使用 angularJS 和材料设计,它不再工作了,我想知道我做错了什么。

标签: angularjs material-design


【解决方案1】:

你有没有尝试在标签&lt;a&gt;中加入href属性?

<a href="#/">Home</a>
<a href="#/about">About</a>
<a href="#/contact">Contact</a>

这样,每当 AngularJS 看到一个 url 变化时,它就会触发路由器去寻找指定的路径。

【讨论】:

  • 是的,我试过了,网址转到 /#/about,但页面内容保持不变,尽管 about.html 有内容
  • 你放了吗?我做了一个例子和它的工作。 codepen.io/marcushenrique/pen/aOzvqG
  • 非常感谢你的例子启发了我
猜你喜欢
  • 1970-01-01
  • 2016-06-15
  • 1970-01-01
  • 2015-10-14
  • 1970-01-01
  • 1970-01-01
  • 2015-07-11
  • 2015-09-22
  • 1970-01-01
相关资源
最近更新 更多