【问题标题】:AngularJS : ui-router components not showingAngularJS:ui-router组件未显示
【发布时间】:2016-10-29 03:49:35
【问题描述】:

我正在尝试编写一个简单的 Angular 应用程序,它使用 Angular ui-router 在我的应用程序中显示组件。

我可以使用模板和控制器让它工作,但是当我重构组件时,它们不会在屏幕上呈现。我也没有收到任何控制台错误。

这是我的应用程序,它基于 Angular 示例应用程序 Hello Solarsytem

app.js

var myApp = angular.module('materialThings', ['ui.router']);

myApp.config(function ($stateProvider) {
    // An array of state definitions
    var states = [
        {
            name: 'about',
            url: '/about',
            component: 'about'
        }
    ]

    // Loop over the state definitions and register them
    states.forEach(function (state) {
        $stateProvider.state(state);
    });
});

myApp.run(function($rootScope) {
    $rootScope.$on("$stateChangeError", console.log.bind(console));
});

index.html

<!DOCTYPE html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>My AngularJS App</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="bower_components/html5-boilerplate/dist/css/normalize.css">
    <link rel="stylesheet" href="bower_components/html5-boilerplate/dist/css/main.css">
    <link rel="stylesheet" href="app.css">
    <script src="bower_components/html5-boilerplate/dist/js/vendor/modernizr-2.8.3.min.js"></script>
    <script src="bower_components/angular/angular.js"></script>
    <script src="bower_components/angular-ui-router/release/angular-ui-router.js"></script>
    <script src="app.js"></script>
    <script src="about.js"></script>

</head>
<body ng-app="materialThings">

<a ui-sref="about" ui-sref-active="active">About</a>

<ui-view></ui-view>

<!-- In production use:
<script src="//ajax.googleapis.com/ajax/libs/angularjs/x.x.x/angular.min.js"></script>
-->

</body>

关于.js

angular.module('materialThings').component('about', {
    template: '<h3>Its the UI-Router<br>Hello Solar System app!</h3>'
})

【问题讨论】:

  • 这是哪个版本的ui-router?

标签: angularjs angular-ui-router angularjs-components


【解决方案1】:

参考这个问题: Angular - UI.Router not loading component

如果您使用的是 0.3.x,那将无法正常工作。升级到 1.0.0(我认为 beta 是最新的),请尝试。

组件属性可从 ui-router@1.0.0 获得(参见 hereCHANGELOG.MD - 它是在 1.0.0-aplpha 中添加的)所以它在 0.3.1 中不可用

另请参阅我对类似帖子的回答 - https://stackoverflow.com/questions/40089948/angular-ui-router-works-with-template-but-not-component

【讨论】:

【解决方案2】:

尝试将您的状态变量更改为:

 var states = [
    {
        name: 'about',
        url: '/about',
        template: '<about></about>'
    }
]

不幸的是 ui-router 组件指南似乎没有这样解释,我会继续寻找更多来告诉你原因。

【讨论】:

猜你喜欢
  • 2020-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-23
  • 2020-01-23
  • 2020-06-13
  • 2017-08-29
  • 1970-01-01
相关资源
最近更新 更多