问题标签 [angular2-services]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
typescript - 获取 Http.get 中的 json 值
我试图在 Angular2 中创建一个小例子。我正在做的是当我单击一个按钮时,这会进入公共 api 并显示一个引号,但我不能,该值永远不会显示,我得到了错误
无法读取 App@4:20 中 [{{quote.contents.quotes.quote}} 中未定义的属性“引号”]
我的组件.ts
我的 boot.ts
索引.html
如何从服务的订阅中获取值并放入我的模板中?
typescript - 将声明设置为 TRUE 时出现打字稿错误
我正在尝试为我正在处理的 Angular 2 项目创建打字稿定义,以便它可以成为可导出的库。
我有几个服务设置,它们将 http 请求返回到与以下内容非常相似的组件:
然后我从这样的组件中调用它:
这一切都很好,直到我在 tsconfig 文件中将“声明”设置为 true,以便我可以创建打字稿定义文件。我的一些服务开始出现以下错误:
我主要了解这个问题,但我不知道解决方案。如果我将 Observable 导入服务,那么 typescript linter 会抛出错误,因为从技术上讲,它没有在该文件中使用。
来自 Angular 1,这是我们在所有应用程序中采用的类似范例来分解我们的代码,但也许我需要更改 Angular 2 中的方法?我查看了许多其他 Angular 2 示例,他们也都以类似的方式完成了它。
modal-dialog - 在 Angular2 中创建模式/对话框系统
有人可以指导我如何在具有大量模态窗口的 angular2 中实现模态系统吗?现在,包含用于搜索客户的组件的模式看起来像这样:
基本上 modal 是一个固定在屏幕中间的简单组件,当 modal 内部的组件发出事件时,我关闭了 modal。在每个show()
和close()
模态Z-index
从服务中获取并应用/删除一些样式。
到目前为止,问题是:
- 按
Esc
我必须关闭最后打开的模式,如何从服务访问该组件?我可以以某种方式传递对服务的引用modal.show()
吗? - 如果服务中发生异常,我如何访问模式来显示我的问题?在我的应用程序的根组件中有一个模态组件(如果是这样,那么我又遇到了第一个问题,如何从服务内部访问它)?
- 我可以像前面的示例一样轻松地从模板打开和关闭模式,
(complete)="searchModal.hide()"
但是如何从类方法内部执行此操作?
有人可以告诉我这样做的正确方法,或者如何解决现有问题吗?
typescript - Pass Page/Global Variables into Angular2 app for use with services
I am looking for some best practice here. My angular2 app will live inside an existing Content Managment System. As a result I need to capture some "variables" generated by this CMS (like auth tokens etc) and use them with http requests inside my angular2 app.
When the index.html page is displayed by the CMS it is pre-parsed by the CMS and some tokens (ie [ModuleContext:ModuleId]) are replaced before the page is sent to the browser.
Here is an example of my index.html page (abreviated):
Specifically the $.ServicesFramework is used for generating valid http web.api requests. I want to capture this in a service that can be injected into each component that uses it.
for example (I am using typescript):
One problem is that the typescript compiler throws error that it cannot find "$" etc. I can force this to work by using declare before the typescript class declaration like the following:
Question:
What is a better way (if exists) of capturing these "global" variable for use in the app that will scale well.
EDIT - update to RC6
I used the following to work in the RC6:
typescript - Angular2 - 使用服务在组件之间共享数据
我有一个我想在我的组件之间共享到 Angular2 应用程序中的对象。
这是第一个组件的来源:
第二个组成部分:
最后是我的小服务 ConfigService:
我的数据没有共享,在grid.component.ts中,该_configService.getConfig()
行返回一个空对象,但它是在app.component.ts之前填充的。
我阅读了文档和教程,没有任何效果。
我错过了什么?
谢谢
解决了
我的问题是我两次注入了我的 ConfigService。在应用程序的引导程序和我使用它的文件中。
我删除了providers
设置并且它起作用了!
jasmine - 在 Angular 2 中测试 http 请求 URL 的正确方法
使用 Angular 2,我设置了一些服务,其中的每个方法都返回不同的 http 请求,如下所示:
我想测试这个方法。我正在测试请求 url、标头和用户。我有点卡在 url 部分,我可以测试实际的 url,但我不知道如何测试它是否是 GET、POST 等……在 Angular 1 中,我们使用了类似 expectGET 或 expectPOST 的东西。到目前为止,这就是我的做法:
知道如何获取请求的类型吗?还是我只是做错了?
angular - Angular 2 引导过程
我想在调用 Angular 2 中的引导方法以在页面加载时引导应用程序之前发出 AJAX 请求。
有没有我可以使用 Angular 来监听的钩子?我知道我可以将它包装在一个普通的 javascript ajax 请求中,但我认为可能有一种使用 Angular 2 执行此操作的标准方法。在启动 Angular 2 应用程序之前,我需要从服务器获取配置文件。
rest - Angular2 使用一个 REST 调用的多个组件
我是 Angular2 应用程序的一部分(我们使用 beta3),问题如下:
通常我们有一个使用一些服务的组件,该服务使用一些休息调用,并且该组件显示数据。伟大的。
但是,我们确实有一个页面,其中包含 6 个以上的组件,所有这些组件都使用相同的 REST 调用...(后端返回所有组件的数据),并且为每个组件调用 6 次 REST 也是没有意义的如果我们做一些客户端缓存会很奇怪。
有什么开箱即用的东西吗?还是处理这种情况的模式?
谢谢。
typescript - Angular 2 HTTP“无法解析‘AppService’的所有参数”
我尝试将 http 提供程序导入服务,但出现以下错误:
无法解析“AppService”(?) 的所有参数。确保所有参数都用 Inject 修饰或具有有效的类型注释,并且“AppService”用 Injectable 修饰。
以下是一些代码片段:
引导.ts
应用服务.ts
我正在尝试调用服务器上的控制器以最终将 JSON 文件加载到数据表中。非常简单的东西,但我加载 Http 模块的方式似乎是错误的。任何帮助都感激不尽。