gpt4 book ai didi

javascript - 在 angularJS 中的两个 View 之间共享数据

转载 作者:行者123 更新时间:2023-12-03 06:28:07 27 4
gpt4 key购买 nike

我有2个 View ,每个 View 都有一个 Controller ,我想在单击“BL”按钮时将数据从View1发送到View2:

这是 View 1: enter image description here

这是 View 2: enter image description here

我尝试使用事件 my try ,但是只有当我点击“BL”按钮时才会激活Controller2我想使用服务将数据从 Controller 1发送到 Controller 2,但我认为这不是我的情况请问有什么帮助吗,我如何将数据从 View1 发送到 View2谢谢

编辑:这是我的第二次尝试:

View 1/ Controller 1:

 $scope.BLButton = function(){


$scope.foo = Service.foo;
Service.foo = '85';

}
.factory('Service', function() {
var Service = {
foo: '85'
};
return Service;
});

View 2/ Controller 2:

//this method is called to display the data in the input Text from a web Service
$scope.parentmethod = function() {
//traitement
$scope.foo = Service.foo;
....
}

我的代码的问题是,当我单击“BL”按钮时,Controleller2 的方法“parentmethod”永远不会被调用

最佳答案

最好的方法是将所有业务逻辑 (BL) 转移到维护应用程序状态的服务中。您试图以某种方式滥用 Controller ,因为它们的唯一目标是粘合演示和 BL,而不是以任何形式保存应用程序状态。问题是 Controller 是在 View 更改时创建和销毁的,因此它们不适合作为状态持有者。在您的情况下,您可以将数据存储在服务中(该服务在您的应用程序运行时始终实例化并处于事件状态),然后在 Controller 中的另一个 View 中检索它。

关于javascript - 在 angularJS 中的两个 View 之间共享数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38557021/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com