2015-11-08 63 views
3

我使用了ng-include,如下圖所示。但它不起作用。請告訴我爲什麼?提前致謝。Ang-ng-include與剃鬚刀頁面不兼容

createOrEditPropertyModal.cshtml

<div ng-include="'propertymanagement/tabs/createPropertyForm.html'"></div> 

我已經試過這樣also.But相同404錯誤。

<div ng-include="'tabs/createPropertyForm.html'"></div> 

createPropertyForm.cshtml

<script type="text/ng-template" id="createPropertyForm.html"> 
    <form name="createPropertyForm" role="form"> 

     //removed for clarity 

    </form> 
</script> 

Soloution樹

enter image description here

注意:當我把它放在同一頁上,然後它工作。但我怎麼能在上面的方式做到這一點? B'cos我想將該代碼放在單獨的.cshtml文件中。

<div ng-include="'createPropertyForm.html'"></div> //on same page where this works well 

回答

10

方法1:OP的回答:

你可以做this.Here是way.You必須給的完整路徑,如下圖所示。

<div ng-include="'~/App/tenant/views/propertymanagement/tabs/createPropertyForm.cshtml'"></div> 

如果您有任何性能問題,然後看看這裏:ng-Include is very slow with the external template

方法2:

你不能包括.cshtml網頁使用NG-包括

它你必須走合適的路由mvc,就像

<div ng-include="'controller/Template/createPropertyForm'"></div> 

這裏createPropertyForm會像一個動作方法的ID部分

public ActionResult Template(string id) 
    { 
    switch (id.ToLower()) 
    { 
     case "createPropertyForm": 
      return PartialView("~/Views/propertymanagement/tabs/createPropertyForm.cshtml"); 

     default: 
      throw new Exception("template not known"); 
    } 
} 

否則只需要創建一個HTML頁面和訪問它作爲

<div ng-include="'createPropertyForm.html'"></div> 
+0

你能告訴我爲什麼這個行爲不同的* .cshtml頁面和* .html頁面?謝謝。 – Sampath

+1

@Sampath [Anguler Docs](https://docs.angularjs.org/api/ng/directive/ngInclude),ng-include Fetches,編譯幷包含一個外部HTML片段。而cshtml是html +服務器代碼。所以首先通過mvc循環它會得到純html然後可以包含它。根據我的理解。 – Viplock

+1

@Sampath'返回View()'或'返回PartialView()'告訴MVC生成正確的HTML並將其發回,'ng-include'可以將其添加到頁面中。希望它能幫助你。 - 謝謝 – Viplock