AngularJS component/scope ui-路由器中的问题
AngularJS component/scope issue in ui-router
我在更改状态后遇到 ui-路由器和 angularjs 作用域的问题 - 新创建的作用域使用旧控制器(反之亦然)。
我使用 AngularJS "angular": "1.5.8"
和 ui-router "@uirouter/angularjs": "^1.0.22"
,组件以 ES6 风格编写为 类。
组件示例:
import template from './my-container.html';
export const MyContainer = {
bindings: {
},
template: template,
controller: class {
constructor($scope) {
'ngInject';
this.$scope = $scope;
...
Route/State定义:
.state('mystate', {
url: '/mystate',
component: 'myPage' // in myPage <my-container>
})
问题:
在首页加载一切正常。但是当我改变状态并返回时 - 任何更改都不会出现在模板中。
我提到 $scope.$id 已更改,但我的控制器仍使用以前的范围对象。
为了调试,我添加了两个控制台打印:在 onInit 和 myUpdateData 函数中。
在第一个屏幕截图上我们可以看到正确的 ID(初始化、更新函数,来自 template/dom 元素):
改变状态后(错误行为):
未来调用 updateData - 它使用旧的(scr 上的 14)范围但呈现新的范围。所以我们没有看到模板有任何变化。
有什么想法可以从这种情况中找出来吗?
已解决。
问题出在组件代码中 - 我忘记删除对先前控制器实例的所有引用,并且在更改状态后该控制器在内存中保持活动状态(我不确定它是如何连接到新范围对象的,但它描述了解决方案).
当你遇到这样的行为时 - 尝试留下几乎空的组件,你会发现错误。
我在更改状态后遇到 ui-路由器和 angularjs 作用域的问题 - 新创建的作用域使用旧控制器(反之亦然)。
我使用 AngularJS "angular": "1.5.8"
和 ui-router "@uirouter/angularjs": "^1.0.22"
,组件以 ES6 风格编写为 类。
组件示例:
import template from './my-container.html';
export const MyContainer = {
bindings: {
},
template: template,
controller: class {
constructor($scope) {
'ngInject';
this.$scope = $scope;
...
Route/State定义:
.state('mystate', {
url: '/mystate',
component: 'myPage' // in myPage <my-container>
})
问题: 在首页加载一切正常。但是当我改变状态并返回时 - 任何更改都不会出现在模板中。
我提到 $scope.$id 已更改,但我的控制器仍使用以前的范围对象。
为了调试,我添加了两个控制台打印:在 onInit 和 myUpdateData 函数中。
在第一个屏幕截图上我们可以看到正确的 ID(初始化、更新函数,来自 template/dom 元素):
改变状态后(错误行为):
未来调用 updateData - 它使用旧的(scr 上的 14)范围但呈现新的范围。所以我们没有看到模板有任何变化。
有什么想法可以从这种情况中找出来吗?
已解决。
问题出在组件代码中 - 我忘记删除对先前控制器实例的所有引用,并且在更改状态后该控制器在内存中保持活动状态(我不确定它是如何连接到新范围对象的,但它描述了解决方案).
当你遇到这样的行为时 - 尝试留下几乎空的组件,你会发现错误。