ASP.NET MVC局部视图在AngularJS指令 [英] ASP.NET MVC Partial View in an AngularJS directive

查看:232
本文介绍了ASP.NET MVC局部视图在AngularJS指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前工作的一个ASP.NET MVC项目在其中加入一些AngularJS - 包括一些AngularJS指令。
我需要添加到AngularJS指令一个MVC局部视图。显然,

I'm currently working on an ASP.NET MVC project to which some AngularJS was added - including some AngularJS directives. I need to add to an AngularJS directive a MVC partial view. Obviously,

@Html.Partial("_PartialView", {{name}}) 

不起作用。

到目前为止,我所有的在线搜索提供任何帮助。

So far all my searches online provided no help.

任何想法,我怎么能渲染角度指令内部的局部视图?

Any idea how I could render a partial view inside an Angular directive?

谢谢!

推荐答案

角严格的客户端,而在服务器端存在的MVC的观点存在。这两个不能直接相互作用。但是,您可以创建在您的局部视图返回HTML的端点。角可以称之为端点,检索HTML,然后将其包含指令中。

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.

事情是这样的:

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
        });
    }  
  };
});

这篇关于ASP.NET MVC局部视图在AngularJS指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆