AngularJS 指令中的 ASP.NET MVC 部分视图 [英] ASP.NET MVC Partial View in an AngularJS directive

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

问题描述

我目前正在开发一个 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.

知道如何在 Angular 指令中呈现局部视图吗?

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

谢谢!

推荐答案

Angular 严格存在于客户端,而 MVC 视图存在于服务器端.这两者不能直接交互.但是,您可以创建一个端点,在该端点中您的部分视图以 HTML 形式返回.Angular 可以调用此端点,检索 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
        });
    }  
  };
});

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

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