我有相同的控制器两种状态如下:没有得到从子状态更新值,angularjs
.state('parent',{
abstract: true,
url: '/parent',
templateUrl: 'mytemplate.html'
controller: 'mycontroller'
})
.state('parent.child',{
url: '/child',
templateUrl: 'mytemplate2.html',
controller: 'mycontroller'
})
而且控制器是:
.controller('mycontroller', ['$scope', function($scope){
$scope.x = 'user';
$scope.check = function(){
alert($scope.x);
}
$scope.checkFromParent = function(){
alert($scope.x);
}
}]
现在mytemplate2.html
我改变的价值变量x
至buyer
。然后我按下一个按钮,调用功能check()
,它产生一个警报,其中x
的值显示为buyer
。
但mytemplate2.html
的x
值更改为buyer
如果我从mytemplate.html
它调用函数checkFromParent()
内按下一个按钮后,警报显示x
作为user
值。为什么?
也许我失去了一些东西非常基本在这里,但更新x
值后,其显示通过调用这两个功能不同。我怎样才能以我想要的方式工作?虽然我可以通过使用服务来完成,但我认为应该有更多的方法来完成它。
mytemplate.html
显示在屏幕的左半部分,mytemplate2.html
显示在屏幕的右侧,使用ui-view
。
感谢,我现在明白了。我可以使用服务,但我想知道是否有其他方法。 –
我不知道是否有任何其他的替代品。 – Pradeepb