问题描述
我想使用ui-Router将一些值从一个视图传递到Angularjs中的另一个视图.
我不想使用$ rootScope来保存数据或创建新服务(因为我有许多视图会传递少量数据,因此为几行代码创建新jsfile并不有趣).我想要做的一个超小型示例是:
I want to pass some values from one view to another in Angularjs using ui-Router.
I don't want to use $rootScope to save data or create a new services ( as I have many views that pass small bits of data so creating new jsfile for few lines code is not fun). A super-minified example of what I want to do is:
视图1的控制器 $scope.goodFood = 10 $scope.badFood = 2
Controller of View 1$scope.goodFood = 10 $scope.badFood = 2
视图2的控制器 $scope.results = 10 - 2 (from view 1's scope)
是否有任何快速的方法来进行此类小型操作?
Is there any quick way to do these kinds of small operations ?
推荐答案
无需为新的数据位创建新服务.只需创建一个带有对象的价值服务:
There is no need to create a new service for new bits of data. Simply create a value service with an object:
app.value("viewData", {});
然后只需根据需要添加新属性:
Then simply add new properties as needed:
app.controller("viewCtrl", function(viewData) {
viewData.newProp = "new info";
console.log(viewData.oldProp);
});
由于价值服务是单例服务,因此对对象内容的更改将在视图更改后仍然存在.
As value services are singletons, changes to the contents of the object will survive view changes.
这篇关于将数据从一个路由视图传递到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!