RealSteel
RealSteel

Reputation: 1941

angular scripts not working while ajax callback

I've a page, where I've defined all the angular coding for accordion. Now, I'm loading that page content using ajax.

My angular page content

angular.module('plunker', ['ui.bootstrap']);

function AccordionDemoCtrl($scope) {

    $scope.groups = [{
        DivisionId: 1,
        Division: '.Net',
        items: [{
            'Id': 1,
                'Name': 'Manikishore',
                'Division': '.Net'
        }, {
            'Id': 2,
                'Name': 'Santosh',
                'Division': '.Net'
        }, {
            'Id': 2,
                'Name': 'Prakash',
                'Division': '.Net'
        }]
    }, {
        DivisionId: 2,
        Division: 'Design',
        items: [{
            'Id': 19,
                'Name': 'Mahesh',
                'Division': 'Design'
        }, {
            'Id': 20,
                'Name': 'Hareen',
                'Division': 'Design'
        }]
    }];
}
<html xmlns="http://www.w3.org/1999/xhtml">
    
    <head runat="server">
        <title></title>
        <script src="JS/test.js"></script>
    </head>
    
    <body>
        <div ng-app="plunker" ng-controller="AccordionDemoCtrl">
            <accordion>
                <accordion-group heading="{{group.Division}}" ng-repeat="group in groups">
                    <accordion-heading> <span ng-click="opened(group, $index)">
                        {{group.Division}}
                    </span>

                    </accordion-heading>
                    <ul class="ulaccordionscroll" style="height:250px;overflow-y:auto;">
                        <li ng-repeat="item in group.items">{{item['Name']}}</li>
                    </ul>
                </accordion-group>
            </accordion>
        </div>
    </body>

</html>

Actually, If I run this script, it's working fine. But in my case, I'm loading this page into another page using ajax on a button click.

Master page

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.js"></script>
<script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.3.0.js"></script>
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet" />

 <a onclick="redirect()">click</a>
 <div id="maindiv" style="background-color:#eee;padding:50px;"></div>

<script>
        function redirect() {
            debugger;
            $.ajax({
                url: '/Default.aspx/',
                type: "GET",
                success: function (response) {
                    debugger;
                    $("#maindiv").empty();
                    $('#maindiv').html(response);
                },
                error: function (err) { debugger; }
            });
        }
    </script>

But the angular code is not working. I've made a sample video describing my error.
Sample Video Link

I've followed this link to get angular accordion

Note : Unfortunately, the code snippet is not working because of the test.js reference

Upvotes: 0

Views: 314

Answers (1)

RealSteel
RealSteel

Reputation: 1941

By @dfsq Comment : we need to bootstrap application manually by providing application root.

I've done the following changes and it worked for me!!!

angular.module('plunker', [])
      .controller('AccordionDemoCtrl', ['$scope', function ($scope) {
          $scope.groups = [
      {
          DivisionId: 1, Division: '.Net',
          items: [{ 'Id': 1, 'Name': 'Manikishore', 'Division': '.Net' },
           { 'Id': 2, 'Name': 'Santosh', 'Division': '.Net' },
           { 'Id': 2, 'Name': 'Santosh', 'Division': '.Net' },
           { 'Id': 2, 'Name': 'Santosh', 'Division': '.Net' }]
      },
      {
          DivisionId: 2, Division: 'Design',
          items: [{ 'Id': 19, 'Name': 'Mahesh', 'Division': 'Design' },
            { 'Id': 20, 'Name': 'Hareen', 'Division': 'Design' }]
      }
          ];
      }]);


angular.element(document).ready(function () {
    angular.bootstrap(document, ['plunker', 'ui.bootstrap']);
});

Upvotes: 1

Related Questions