将“控制器作为"与 ui-router 一起使用未按预期工作 [英] Using 'controller as' with the ui-router isn't working as expected

查看:20
本文介绍了将“控制器作为"与 ui-router 一起使用未按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用抽象状态和控制器作为语法为页面设置了以下状态:

# 详情页路由.state '标题',网址:'/标题',摘要:真实,模板:'<ui-view/>',.state 'title.show',url: '/:titleId',templateUrl: 'title/show.html'控制器:'标题为 t'

为了这个演示的目的,假设我将一个变量分配给 'Title' 控制器的 't' 实例,我在 Title 控制器函数内部执行此操作.

angular.module('title').controller 'Title',['$state', ($state) ->this.name = '测试'

在我的视图title/show.html"中,我尝试将我刚刚创建的变量打印到页面上:

{{t.name}}

我什么都没看到.但是,如果我将 ui-router 的控制器移到包装title/show.html"页面的元素上,如下所示:

然后一切正常.有没有人遇到过这个问题.我在另一个应用程序中运行良好,所以我仍在尝试弄清楚这个应用程序中可能有什么不同,也许是 js 库版本的不同.

解决方案

在您的状态配置中:

代替 controller: 'Title as t',尝试:

controller: 'Title',控制器为:'t'

Edit :刚刚使用 ui-router 实现了一个最小的应用程序,语法 controller: Title as t 在 0.2 版本中也有效.ui-router 的 0 到今天的最新一个.当我检查角度范围时,我可以看到 t 实例.

I have the following state setup for a page using an abstract state and the controller as syntax:

# Details page route
.state 'title', 
  url: '/title',
  abstract: true,
  template: '<ui-view/>',
.state 'title.show', 
  url: '/:titleId',
  templateUrl: 'title/show.html'
  controller: 'Title as t'

For the purpose of this demo lets say I assign a variable to the 't' instance of the 'Title' controller and I do this inside of the Title controller function.

angular.module('title').controller 'Title',
 ['$state', ($state) ->
   this.name = 'Test'

and inside my view 'title/show.html' I attempt to print out that variable I just created to the page:

{{t.name}}

I don't see anything. But if I remove the controller our of the ui-router and onto the element that wraps the 'title/show.html' page like this:

<div ng-controller="Title as t">

Then everything works great. Has anyone come across this problem before. I have it working fine in another app so I'm still trying to figure out what might be different in this app, maybe a difference in js library versions.

解决方案

In your state configuration :

Instead of controller: 'Title as t', try :

controller: 'Title',
controllerAs: 't'

Edit : Just implemented a minimal app with ui-router and the syntax controller: Title as t also works, in versions 0.2.0 of ui-router to the most recent one as of today. I can see the t instance when I inspect angular scopes.

这篇关于将“控制器作为"与 ui-router 一起使用未按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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