Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reload DOM after AJAX queries AngularJS

In my project, I do an AJAX request using AngularJS who call another page that includes Angular directives (I want to make another AJAX call inside) but no interaction in loaded page.

I think new DOM isn't functionnal.. I'm trying the pseudo-code $apply unsuccessed.

Main.html :

<!DOCTYPE html>
<html data-ng-app="App">
    <head></head>
    <body >
        <div data-ng-controller="editeurMenuMobile">
            <ul>
                <li data-ng-click="callMenu('FirstAjax.html')" > <!-- Work ! -->
                    <a href="">
                        <span>Modèles</span>
                    </a>
                </li>
                <li data-ng-click="callMenu('FirstAjax.html')"> <!-- Work ! -->
                    <a href="">
                        <span>Designs</span>
                    </a>
                </li>
            </ul>
            <div data-ng-bind-html="data">
                <!-- AJAX content -->
            </div>
        </div>

        <!-- Javascript scripts -->
    </body>
</html>

FirstAjax.html :

<div data-ng-controller="editeurActionAjax">
    <div>
        <button data-ng-click="callAction('SecondAjax.html')"> <!-- Doesn't work -->
            Go
        </button>
    </div>
</div>

And my JS :

var App = angular.module('App', []);

App.controller('editeurMenuAjax', ['$scope', '$http', '$sce', function($scope, $http, $sce) {
        $scope.callMenu = function(element) {
            $http({method: 'GET', url: element}).
                    success(function(data) {
                        $scope.data = $sce.trustAsHtml(data);
                    }).
                    error(function() {
                        $scope.showAjaxError = true;
                    });
        };
    }
]);
App.controller('editeurActionAjax', ['$scope', '$http', '$sce', function($scope, $http, $sce) {
        $scope.callAction = function(element) {
            $http({method: 'GET', url: element}).
                    success(function(data) {
                        $scope.data = $sce.trustAsHtml(data);
                    }).
                    error(function() {
                    });
        };
    }
]);

Thank you for your help

like image 493
JohnnyC Avatar asked Jan 21 '26 07:01

JohnnyC


1 Answers

From my point of view could the problem be because of the $scope?

your 2nd Controller don't have access to the same data variable.

Try to change the code to use $rootScope in both controllers instead of $scope, and see if it fix the problem.

Or

On your FirstAjax.html insert this:

<div data-ng-bind-html="data">
    <!-- AJAX content -->
</div>

This should make a second data variable inside Scope of Controller 2, so that it can place the content.

like image 78
1st4ck Avatar answered Jan 23 '26 02:01

1st4ck