LoginSignup
5
5

More than 5 years have passed since last update.

One-time bindingと$resourceで気をつけること

Last updated at Posted at 2015-01-08

前提

AngularJS1.3からOne-time bindingという最初の一回だけバインディングする仕組みが入った。
$resourceはpromiseではなく、先に空配列を返す。

問題

なので、下記のようにngRepeatをOne-time bindingにすると、空配列評価されて、何も表示されなくなったりする。
あ、下のindex()はquery()と読み替えて問題なし。

sample.coffee

angular.module("myApp")
.controller "SampleCtrl", ($scope, Group) ->
  $scope.groups = Group.index()

index.html
<div  ng-repeat="group in ::groups">
{{::group.name}}
</div>

解決

プロミスで処理しよう。
個人的には$resourceは出来るだけpromiseにしたほうがいいと思う。

sample.coffee

angular.module("myApp")
.controller "SampleCtrl", ($scope, Group) ->
  Group.index().$promise.then((groups)->
    $scope.groups = groups
  )
5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5