在ui路由器中使用"controller as"无法正常工作 [英] Using 'controller as' with the ui-router isn't working as expected
问题描述
我使用抽象状态和控制器作为语法对页面进行以下状态设置:
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'
就本演示而言,假设我将一个变量分配给标题"控制器的"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'
在我的视图"title/show.html"中,我尝试将刚刚创建的变量打印到页面上:
and inside my view 'title/show.html' I attempt to print out that variable I just created to the page:
{{t.name}}
我什么也没看到.但是,如果我删除了ui路由器的控制器,然后放到包裹"title/show.html"页面的元素上,就像这样:
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">
然后一切正常.有人遇到过这个问题吗?我在另一个应用程序中可以正常工作,因此我仍在尝试找出此应用程序中可能有什么不同,也许是js库版本中的不同.
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.
推荐答案
在您的状态配置中:
而不是controller: 'Title as t'
,请尝试:
controller: 'Title',
controllerAs: 't'
编辑:刚刚使用ui-router
实现了一个最小的应用程序,语法controller: Title as t
也可以使用,从ui-router
的0.2.0版本更新到今天的最新版本.检查角度范围时,可以看到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路由器中使用"controller as"无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!