将数据从一个路由视图传递到另一个视图 [英] Passing data from one route view to another

查看:33
本文介绍了将数据从一个路由视图传递到另一个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Angularjs 中使用 ui-Router 将一些值从一个视图传递到另一个视图.
我不想使用 $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

视图 2 的控制器
$scope.results = 10 - 2(来自视图 1 的范围)

有没有什么快速的方法可以做这些小操作?

Is there any quick way to do these kinds of small operations ?

推荐答案

我不想使用 $rootScope 来保存数据或创建新服务(因为我有很多视图传递少量数据,因此为几行代码创建新的 jsfile 并不好玩)

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)

无需为新的数据位创建新服务.简单地用一个对象创建一个价值服务:

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.

这篇关于将数据从一个路由视图传递到另一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆