Angularjsのng-repeatの描画終了後に何らかの処理をする
Angularjsのng-repeat終了後にfunctionを実行したい場合が多分あると思います。
https://docs.angularjs.org/api/ng/directive/ngRepeat
上記Documentを読むと$lastがあるのですが、これをdirectiveでそのまま使うとまだ描画が終わっていないので、上手く動きません。
# slim ul ng-controller="HogesController as hoges" li ng-repeat="hoge in hoges.list" hoge-on-last-repeat='' .hoge id="hoge-{{hoge.id}}"
# coffee script angular.module('module-name').directive("hogeOnLastRepeat", [ () -> return ($scope, $element, $attrs) -> if $scope.last # ここで処理を書く ])
$scope.last
でconsole.log('#hoge-123').attr('id')
とかしても'hoge-{{hoge.id}}
が返ってきます。まだ描画されていないです。
なのでdirectiveをこのようにしてあげます。
# coffee script angular.module('module-name').directive("hogeOnLastRepeat", ['$timeout' ($timeout) -> return ($scope, $element, $attrs) -> if $scope.last $timeout( () -> # ここで処理を書く ) ])
これでちゃんと描画された後に処理が走りますヽ(´ー`)ノ