On the navigation bar, I have a shopping cart snapshot (total items, total price) which uses $scope.cart
.
在导航栏上,我有一个使用$ scope.cart的购物车快照(总项目,总价格)。
In the ng-view
, there are pages to add/remove items in the shopping cart.
在ng-view中,有一些页面可以在购物车中添加/删除项目。
It appears that ng-view
created an isolated scope. If I add items into $scope.cart
, it goes to the scope that belongs to ng-view
. How can ng-view
share the parent scope?
似乎ng-view创建了一个孤立的范围。如果我将项添加到$ scope.cart中,它将转到属于ng-view的范围。 ng-view如何共享父范围?
1 个解决方案
#1
1
The $scope
exists inside the ng-controller
. The ng-view
directive only tells to Angular JS the place where partials will be loaded. You have couple of ways to archive this cart in another place outside, as:
$ scope存在于ng-controller内。 ng-view指令仅向Angular JS告知将加载部分的位置。您可以通过多种方式将此购物车存放在外面的其他位置,如:
1) You can create an outter contoller with $scope.cart
and propagate from the inner controller the value, using the same variable name.
1)您可以使用$ scope.cart创建一个outter控制器,并使用相同的变量名从内部控制器传播该值。
- AngularJS–Part 3, Inheritance
AngularJS-第3部分,继承
2) Another way could be using a Service
, that's singleton in Angular JS, to set the cart and then change it in the inner controller, to propagate to the outter one.
2)另一种方法是使用服务,即Angular JS中的单例,设置购物车然后在内部控制器中更改它,以传播到外部控制器。
- Passing data between controllers in Angular JS?
在Angular JS中的控制器之间传递数据?
#1
1
The $scope
exists inside the ng-controller
. The ng-view
directive only tells to Angular JS the place where partials will be loaded. You have couple of ways to archive this cart in another place outside, as:
$ scope存在于ng-controller内。 ng-view指令仅向Angular JS告知将加载部分的位置。您可以通过多种方式将此购物车存放在外面的其他位置,如:
1) You can create an outter contoller with $scope.cart
and propagate from the inner controller the value, using the same variable name.
1)您可以使用$ scope.cart创建一个outter控制器,并使用相同的变量名从内部控制器传播该值。
- AngularJS–Part 3, Inheritance
AngularJS-第3部分,继承
2) Another way could be using a Service
, that's singleton in Angular JS, to set the cart and then change it in the inner controller, to propagate to the outter one.
2)另一种方法是使用服务,即Angular JS中的单例,设置购物车然后在内部控制器中更改它,以传播到外部控制器。
- Passing data between controllers in Angular JS?
在Angular JS中的控制器之间传递数据?