1

I am creating an ASP.net MVC application.

I am loading a partial view in a div using jQuery and AngularJS as following:

I am doing an ajax call using AngularJS and storing the result as the content in div "courseList" using jQuery. This happens when the user clicks on links.

  angular.module('Domain', []).controller('DomainCtrl', function (
    $http, $scope, $window) {
    $scope.GetCourseList = function (domain) {
      $http({
        method: 'GET',
        url: '/Showcase/CourseList',
        params: {
          domain: domain
        }
      }).then(function (obj) {
        $('#courseList').html(obj.data);    //jQuery code to load the content
      });
    };
  });

Html:

<nav ng-app="Domain">
  <h2>Domains</h2>
  <ul ng-controller="DomainCtrl" ng-init="domain=''">
    @foreach (var item in (List<string>)ViewBag.domains)
    {
      <li><a href="#" ng-click="GetCourseList('@item.ToString()')">@item.ToString() </a></li>
    }
  </ul>
</nav>

<div id="courseList">    </div>

The above code works fine. But I am using jQuery to populate the <div ID="courseList">.

How can I achieve this with AngularJS and no jQuery?

1
  • obj.data will have the contents of the partial view say <div><table>... this will be inserted into the div courseList Commented Jul 14, 2014 at 9:12

3 Answers 3

1

You can replace calls to jQuery $ dollar e.g.

$('#courseList').html(obj.data);

with Angular's equivalent which is angular.element

angular.element('#courseList').html(obj.data);

Under the hood though, Angular will use jQuery if it's available i.e. you are including a version of jQuery in a <script> tag somewhere. Otherwise Angular will use jqLite, which is internally available as part of Angular itself.

Sign up to request clarification or add additional context in comments.

6 Comments

it's working, can you explain a bit about the solution? How it works?
Have a read of the documentation for angular.element, which I included the url in my updated answer. That page should explain everything for you.
this is off topic but how can I load a popup kind of view inside the current page using AngularJS where I can do a form submit inside the popup window?
@RandomUser You're much better off asking that as a new question. It's best not to mix two questions on one page.
It's hard to help you with that question without seeing any existing code you have. If you have a popup window, then you need a <form> element in that window to do a submit. Or you could use ajax to POST data back to the server. I can't see why people would downvote that question, it seems reasonable enough.
|
1

To do this in Angular way. You should declare expression in the controller scope.

HTML:

<div id="courseList">{{ courseData }}</div>

And in your then callback, replace jQuery code with this:

$scope.courseData = obj.data;

Also set the initial state of courseData before $scope.GetCourseList method.

$scope.courseData = "";

Comments

1
<div id="courseList" ng-bind-html="courseListHtml"></div>

And in your controller:

$http(...).then(function (obj) {
    $scope.courseListHtml = $sce.trustAsHtml(obj.data);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.