Search code examples
angularjsrestviewangular-ui-routerpartial

Dynamic loading of AngularJS view


Is there a way to load the view that is exposed via Rest with UI-Router in AngularJS? The idea behind this is to have UI-Router request the partial from the Rest interface and then have the controller add the variables to $scope.


Solution

  • Assume 2 pages, index.html and details.html

    in index.html

    <script>
    function ctr($scope,$http)
    {
      $http({
        method: 'GET',
        url: "details.html"
        }).success(function (data, status){
          $("#content").empty();
          $("#content").append(x);
          var p = $("#content");
          $compile(p.contents())($scope);
        });
    }
    </script>
    <div id="content">
    put ur content here
    </div>
    

    in details.html

    <script>
    function ctrb($scope)
    {
      $scope.details = "here is details";
    }
    </script>
    <div ng-controller="ctrb">
      {{details}}
    </div>
    

    So the details will have own controller, and main controller also can access detail controller if u want.