[英]angularjs - sharing value between factories
Hi Below is the code for a controller in Angular i am working on.嗨,下面是我正在处理的 Angular 控制器的代码。 I am referencing svcSPoint which is a factory registered to the module.
我正在引用 svcSPoint,它是注册到模块的工厂。 What i cant figure is this.
我想不通的是这个。 I am able to access svcSPoint is in the constructor as the ng-annotate is injecting the factory.
我能够访问 svcSPoint 在构造函数中,因为 ng-annotate 正在注入工厂。 However, i cant access the svcSPoint in the function SelectChanged()
但是,我无法访问 SelectChanged() 函数中的 svcSPoint
What i am trying to accomplish is to set the value of a factory when an event fires in DOM.我想要完成的是在 DOM 中触发事件时设置工厂的值。 How can i capture a value in global so i can access it in a bunch of controllers and factories.
我怎样才能在全局中捕获一个值,以便我可以在一堆控制器和工厂中访问它。
'use strict';
class NavbarController {
//start-non-standard
menu = [{
'title': 'Home',
'state': 'main'
}];
isCollapsed = true;
//end-non-standard
constructor(Auth, svcSPoint) {
this.isLoggedIn = Auth.isLoggedIn;
this.isAdmin = Auth.isAdmin;
this.currentuser = svcSPoint.currentuser;
this.scopeSelect = 'NAL';
this.scopeSelects = ['NAL', 'USL', 'CAD'];
svcSPoint.scopeSelection = this.scopeSelect;
this.svcSPoint = svcSPoint;
}
SelectChanged() {
svcSPoint.scopeSelection = this.scopeSelect;
}
}
angular.module('nalAngularDashApp')
.controller('NavbarController', NavbarController);
For anyone who has the same problem and starting to learn angular.对于任何有同样问题并开始学习 angular 的人。 My code started working when i changed it from factory to service.
当我将代码从工厂更改为服务时,我的代码开始工作。 since service is instantiated as a class every time it retains all the global values.
因为服务在每次保留所有全局值时都被实例化为一个类。 I am using service to share data between controllers and directive.
我正在使用服务在控制器和指令之间共享数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.