問題タブ [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 - 異なるコントローラーに属する2つのメソッドを実行するng-click

ボタンがクリックされとき両方
関数実行
たい

ネストされたコントローラーの概念を指定せずにそれを行うにはどうすればよいですか

0 投票する
0 に答える
247 参照

ruby-on-rails - AngularJS と Rails を使用したファイルのアップロード

BOOKangularjsコントローラーを介して本の詳細を挿入するモデルがあります

これが私の見解です:

私のangularjsコントローラー機能は次のとおりです。

問題は、書籍の pdf アップロード以外のすべての詳細が保存されていることです。名前すら渡さない。どうすればこれを解決できますか? 任意の提案をいただければ幸いです。

0 投票する
1 に答える
89 参照

angularjs - 注入された angular モジュールのサービスをアプリで使用するにはどうすればよいですか?

アプリにさまざまな角度モジュールを挿入しようとしています。私が作成した 1 つのモジュールには、コントローラーに挿入したいサービスが含まれています。誰かがこれを行う方法の良い説明を持っていますか?

私のapp.js:

注入したいサービスを含む myModule:

このコントローラーに「coolService」を正しく挿入する方法の例は、私の質問にうまく答えます。

0 投票する
2 に答える
170 参照

javascript - Array Null 外側 loop 、内側は Angular.js サービスの値

これがばかげた質問/ぎこちない言い回しのように聞こえる場合はご容赦ください。したがって、私のサービス関数は次のとおりです。関数の外側に配列変数があります

次に、次のように機能します。

前もって感謝します

更新しました


ここに完全なコードがあります..

そして、次のようにコントローラーから私の呼び出し

0 投票する
1 に答える
1245 参照

javascript - 角度テスト: 角度付きサービスをプライベート メソッドでテストする

サービスでこの種のメソッドを取得したとしましょう:

しかし、基本的なテストを実行しています。

句を匿名関数に変更すると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: