ASP.NET MVC Partial View in an AngularJS directive

纵饮孤独 提交于 2019-12-03 20:25:03

Angular exists strictly on the client side whereas MVC views exist on the server side. These two cannot interact directly. However, you could create an endpoint in which your partial view is returned as HTML. Angular could call this endpoint, retrieve the HTML, and then include it inside a directive.

Something like this:

app.directive("specialView", function($http) {
  return {
    link: function(scope, element) {
      $http.get("/views/partials/special-view") // immediately call to retrieve partial
        .success(function(data) {
          element.html(data);  // replace insides of this element with response
        });
    }  
  };
});
app.directive("myDirective", ['', function () {
 return {
      restrict: 'A',
        scope: {
            foo: '='
        },
        templateUrl: '/home/_myDirectivePartialView',
        }]
    } }]);

Just need to use templareURL and specify the route to get the partial view.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!