angularjs 中的 stateprovider - 不呈现 ui-view [英] stateprovider in angularjs - not rendering the ui-view

查看:31
本文介绍了angularjs 中的 stateprovider - 不呈现 ui-view的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的.我被这部分困了两天.我已经尝试了我所知道的一切.我已经加载了 ui-router 并定义了 stateprovider.我的用户界面如网站中的几个示例所示.但是当我运行时,它没有显示我的 ui-view.

索引.html

<div><div data-ng-include="'app/layout/shell.html'"></div>

<!--Angular--><script src="scripts/angular.js"></script><script src="scripts/angular-ui-router.js"></script>

Shell.html

贝壳<div ui-view></div>

条目.html

入口</节>

app.js

var app = angular.module('ngBRep', ['ui.router']);

routes.js

var app = angular.module('ngBRep');app.config(['$stateProvider', '$urlRouterProvider', routeConfigurator]);功能路由配置器($stateProvider,$urlRouterProvider){$urlRouterProvider.otherwise("/");$stateProvider.状态('/', {网址:'/',templateUrl: 'app/views/entry.html'}).state('个人资料', {网址:'/个人资料',templateUrl: 'app/views/profile.html'});//如果未找到 URL,则发送到登录}

我的期望是当我浏览到 index.html 时,我应该看到贝壳简介

但我只得到 SHELL.

最糟糕的是,我们的项目中有一个现有的网站可以做到这一点并且可以正常工作.

任何帮助将不胜感激.谢谢.

解决方案

有一个有效的 plunker.这种情况下的问题与以下事实有关,即我们使用了两个功能(实际上这不是预期的/不是设计为一起工作的):

  • ng-include - 填充 root 视图 (通常是 index.html 的一部分)
  • ui-router - 以尚未加载的 root 为目标
  • 事实上,include 的执行晚于 ui-router 默认调用 $urlRouterProvider.otherwise("/"))

所以虽然这个模板被包含在根 index.html 中

贝壳<div ui-view></div>

我们必须在其控制器中执行此操作:

app.controller('shell', function($scope, $state, ....){$state.go("/");});

通过这种方式,我们解决了加载时间的差异(路由器准备好然后包含模板,因此错过了目标)

检查此处

Alright. I am stuck with this part for two days. I have tried everything i know. I have my ui-router loaded and stateprovider defined. I have the UI as shown in several examples in website. But when i run, it is not showing my ui-view.

Index.html

<body  ng-app='ngBRep'>
<div>
    <div data-ng-include="'app/layout/shell.html'"></div>
</div>
<!--Angular-->
<script src="scripts/angular.js"></script>
<script src="scripts/angular-ui-router.js"></script>
</body>

Shell.html

<div data-ng-controller="shell as vm">
    SHELL
    <div ui-view></div>
</div>

Entry.html

<section id="entry-view" data-ng-controller="entry as vm">
    ENTRY
</section>

app.js

var app = angular.module('ngBRep', [
        'ui.router' 
]);

routes.js

var app = angular.module('ngBRep');

app.config(['$stateProvider', '$urlRouterProvider', routeConfigurator]);

function routeConfigurator($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise("/");
    $stateProvider
      .state('/', {
          url: '/',
          templateUrl: 'app/views/entry.html'
      })
        .state('profile', {
            url: '/profile',
            templateUrl: 'app/views/profile.html'
        });
    // Send to login if the URL was not found

}

My expectation is when i browse to index.html, i should see SHELL PROFILE

But i get only SHELL.

And the worst part, there is an existing website in our project that does exactly this and it works.

Any help will be appreciated. Thank you.

解决方案

There is a working plunker. The issue in this case is related to the fact, that we are using two features (which are in fact not expected / not designed to be working together):

  • ng-include - to populate the root view (usually part of index.html)
  • ui-router - to target the root, which is not loaded yet
  • and in fact that include is executed later than the ui-router default call to$urlRouterProvider.otherwise("/"))

So while this template is included into the root index.html

<div data-ng-controller="shell as vm">
    SHELL
    <div ui-view></div>
</div>

We have to do this in its controller:

app.controller('shell', function($scope, $state, ....){
   $state.go("/");
});

This way we solve the difference in loading times (router is ready sooner then included template, so missing the targets)

Check that here

这篇关于angularjs 中的 stateprovider - 不呈现 ui-view的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆