javascript - AngularJS and getting ng-click to work on dynamically loaded html -


i wonder if little here. trying build small image gallery using angularjs , laravel. far working out great. problem having when dynamically load 1 of html files, ng-click directive not seem work on dynamic content. have looked , came across $compile option, cannot life of me see how can implement it.

any 1 have idea of need do? have attached code working below:

controller code

app.controller('myphotos', function ($scope, $timeout, $http, $document, $modal, $log, $sce, $compile) {  $scope.loadalbums = function () {      $scope.loading = "<div class='text-center'><strong>...loading...</strong></div>";     $scope.isloading = true;      $http({         method: 'get',         url: '/my/photos/fetch/albums'     }).success(function (data) {         var htmlcontent = $compile(data.albums)($scope);         $scope.gallerycontent = $sce.trustashtml(data.albums);         $log.log(data.albums);         $scope.isloading = false;     }).error(function (data, status, headers, config) {         //$scope.progress = data;         $scope.messages = 'there network error. try again later.';         //$log.error(data);         $scope.isloading = false;     }); }; 

base html page

<div class="row"> <div class="col-md-12">     <ul class="list-inline">         <li><a href="#photos-all" class="photoslist">your photos</a></li>         <li><a href="#albums" class="albumslist" data-ng-click="loadalbums();">your albums</a></li>     </ul> </div> </div> <div class="row"> <div class="col-md-12">     <div ng-show="isloading" class="text-center">         <img src="{{ theme::asset()->url('img/loading_balls.gif') }}" />     </div>     <div class="photocontainer" data-ng-bind-html="gallerycontent">         [[ gallerycontent ]]     </div> </div> </div> 

dynamically loaded html

<div class="row"> <div class="col-md-3">     <img src="http://placehold.it/700x400" alt="" class="img-responsive"  />     <a href="#"><span class="fa fa-plus"></span> create album</a> </div> @foreach($albums $album)     <div class="col-md-3">         <img src="http://placehold.it/700x400" alt="" class="img-responsive"  />         <a href="#" data-ng-click="loadalbum({{ $album->id }})" title="{!! $album->title !!}"            data-album-id="{{ $album->id }}">{!! $album->title !!}         </a>         <p>&nbsp;</p>     </div> @endforeach 

the data-ng-click directive not working when content loaded dynamically.

thanks

you can in simpler way, using nginclude:

<div class="photocontainer" ng-include="'/my/photos/fetch/albums'">     <img src="{{ theme::asset()->url('img/loading_balls.gif') }}" /> </div> 

see https://docs.angularjs.org/api/ng/directive/nginclude


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -