问题标签 [angular-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.

0 投票
3 回答
167 浏览

javascript - Angular 的全局可用变量

我们正在将我们的 webapp 迁移到 Angular.js。传统上,我们创建了一个名为的全局对象app,并用它来存储全局使用的函数、变量等。例如,活动用户的姓名和头像可能存储在app.user.

随着向 Angular 的过渡app,它不再只是一个对象,它是一个 Angular 应用程序模块。以 Angular 思维方式在全球范围内存储数据的正确方法是什么?它可能完全不同,那很好。我只是想确保我们做得正确。

我会做一些简单的事情app.global = {};app.global在网站周围使用,但如果 angular API 更改为使用名为 的属性global,那么修复将是一场噩梦。

0 投票
1 回答
2664 浏览

angularjs - ng-click 执行属于不同控制器的 2 个方法

我有一个名称为PostControllerCommentController
PostController的控制器具有功能showPost CommentController
具有功能showComment
我想在单击按钮时执行这两个功能

在不指定嵌套控制器概念的情况下如何做到这一点

0 投票
0 回答
247 浏览

ruby-on-rails - 使用 AngularJS 和 Rails 上传文件

我有一个模型BOOK,将通过 angularjs 控制器插入书籍详细信息

这是我的看法:

我的 angularjs 控制器功能是:

问题是它存储了除书籍 pdf 上传之外的所有详细信息。它甚至没有通过名字。我该如何解决这个问题?任何建议,将不胜感激。

0 投票
1 回答
89 浏览

angularjs - 如何在我的应用程序中使用注入的 Angular 模块的服务?

我正在冒险将不同的角度模块注入我的应用程序。我创建的一个模块包含我想注入控制器的服务。有人对如何做到这一点有很好的解释吗?

我的 app.js:

myModule 包含我要注入的服务:

如何正确地将“coolService”注入此控制器的示例将很好地回答我的问题:

0 投票
2 回答
320 浏览

javascript - 如何在初始请求中提供 JSON 有效负载,以便在 JS 应用程序中使用

使用初始请求为我的 Angular 应用程序中的模型提供初始 JSON 数据的最佳方式是什么?(尽量减少对服务器的请求量)。

我应该直接将数据添加到服务器上的服务中,提供并包含一个单独的文件,还是有更“角度”的方法?

0 投票
2 回答
5970 浏览

javascript - 角度服务/工厂未定义错误

我建立了这个工厂服务:

假设返回我在顶部声明的 usersObject 但控制台给了我一个未定义的对象错误。

这是控制器:

我对 Angular 很陌生。

0 投票
2 回答
170 浏览

javascript - Array Null out side loop ,但在 Angular.js 服务中包含值

如果这听起来像一个愚蠢的问题/尴尬的措辞,请原谅我,所以我的服务函数如下,我在函数之外有一个数组变量

然后功能如下,

提前致谢

更新


这是完整的代码..

我来自控制器的电话如下

0 投票
1 回答
1245 浏览

javascript - Angular 测试:使用私有方法测试 Angular 服务

假设我在服务上得到了这种方法:

但是运行一个基本的测试。

当我将then子句更改为匿名函数时,一切都按预期工作,但是当我使用命名的私有函数时,我得到: 业力打印:

TypeError: 'undefined' 不是函数(评估 '_onRestResult.bind(this,deferred)')

我知道这种答案

但它们指的是控制器而不是服务 (:)) 并建议不要使用私有方法,但我们真的更喜欢使用它们。

我也遇到了这个答案,暗示私有方法已经过隐式测试,这就是我的想法,直到我遇到这个错误消息。

谢谢您的帮助!

编辑:我应该注意到我像这样嘲笑 Restangular(如果这是相关的):

0 投票
2 回答
1677 浏览

angularjs - Angularjs UI won't update with service binding that's updated by promise?

I've spent the night on trying to figure this out and have finally decided to give up and ask for help.

I'm building a web-app with AngularJS that is designed to work with flakey connections (mobiles).

I'm trying to implement the functionality for a user to add a object (whether that's an appointment, book, etc is irrelevant) to the server.

Service that handles syncing objects with the server:

'app' service that the status bar is bound to:

Template binding to the 'app' service inSync property:

Specific object service that sends data from the controller to the syncUp service:

Everything is working as it should (data is being persisted to the server and the ID is being returned and replacing the tempId just fine. The problem is, when the inSync key on the 'app' service is updated, the class isn't added/removed from the div as it should be with ng-class in the template. If I load another route, that will force iterate through whatever internal cycle angular is doing and update the class on the template.

I've tried all manner of $apply() solutions, moving where the app.inSync key is set back to true, looping a function watching it. It's being set in all the right places (from debugging I know it's set back to true correctly), I just can't figure out how to make the change appear on the UI.

I tried: $rootScope.$apply(function() { app.inSync = true; });

Which gave me an error (already running a digest, or something).

So I tried the 'safeApply' version that has been circulated on many answers/blogs, which didn't throw the error, but didn't work either.

As far as I can figure out, the UI should be updated when promises are resolved (both the http and my syncUp.addObject promise are resolved, so I'm not sure why it's not working.

Any ideas? I need to keep the current implementation of promises to be able to set the returned ID from the server on the added object, to avoid a circular-dependency issue between the syncUp and object angular services.

Edit:

And the status bar directive:

0 投票
1 回答
158 浏览

javascript - Angularjs如何调用异步服务方法以及如何在监听器中使用依赖

我想在侦听器中使用依赖项,但 websocket 是undefined

并且想要在准备好时调用服务方法(依赖于异步方法)

谢谢你。

jsfiddle http://jsfiddle.net/8DHfY/3/或此处的代码