2017-05-27 42 views
1

我有相同的控制器两种状态如下:没有得到从子状态更新值,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我改变的价值变量xbuyer。然后我按下一个按钮,调用功能check(),它产生一个警报,其中x的值显示为buyer

mytemplate2.htmlx值更改为buyer如果我从mytemplate.html它调用函数checkFromParent()内按下一个按钮后,警报显示x作为user值。为什么?

也许我失去了一些东西非常基本在这里,但更新x值后,其显示通过调用这两个功能不同。我怎样才能以我想要的方式工作?虽然我可以通过使用服务来完成,但我认为应该有更多的方法来完成它。

mytemplate.html显示在屏幕的左半部分,mytemplate2.html显示在屏幕的右侧,使用ui-view

回答

0

他们每个人都有自己的控制器的情况下,这样的数据是 不同的两个

要使其工作,你应该使用service在它们之间共享数据。

愿这example可能会给你一些想法。

+0

感谢,我现在明白了。我可以使用服务,但我想知道是否有其他方法。 –

+0

我不知道是否有任何其他的替代品。 – Pradeepb

0

您正在使用两个HTML模板,但两者控制范围相同的控制器是不同的,这就是为什么你看到如此。