2013-10-21 113 views
0

我使用的是angularJs,twitter引导与asp.net mvc4。我没有使用角带。我试图在bootstrap模式下加载部分视图。局部视图包含角码。角码不工作在引导模式

这里是我的代码,其中我打电话的引导模式:

@model IEnumerable<IMS.Domain.Model.GetAllRequisitions_Result> 

@{ 
    ViewBag.Title = "Requisition List"; 
    Layout = "~/Views/Shared/MasterPage.cshtml"; 
} 

<a href="#myModal" role="button" class="btn btn-primary" id="modalluncher" data-toggle="modal" onclick="loadCreateForm()"> Launch demo modal </a> 

<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> 

    <div class="modal-body"> 
    </div> 

</div> 

<script type="text/javascript"> 
    function loadCreateForm() { 
     var content = '@Url.Action("Create", "Requisition", new { area = "IMS" })'; 
     $('div.modal-body').load(content); 
} 
</script> 

这里是我的代码为 '征用' 控制器 '创建' 行动:

<div class="modal-header"> 
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> 
    <h3 id="myModalLabel">Modal header</h3> 
</div> 

<div ng-app=""> 
    <label>Sample: <input type="text" ng-model="sample" /></label> 
    <br /> 
    <label>Show Value: {{sample}}</label> 
</div> 

<div class="ContainerRowButton"> 
    <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button> 
</div> 

对于ng-model =“sample”,当我试图按{{sample}}显示数据时,它保持{{sample}}。这就像我的'创建'行动没有得到angularJs。

我已经在'MasterPage.cshtml'中提供了所有必要的脚本。我MasterPage.cshtml包含:

@Scripts.Render("~/bundles/jquery") 
@Scripts.Render("~/bundles/jqueryui") 
@Scripts.Render("~/bundles/others") 

这里是我的 'Bundle.config.cs' 文件的代码:

bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
        "~/Scripts/jquery-{version}.js")); 

bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
        "~/Scripts/jquery-ui-{version}.js")); 

bundles.Add(new ScriptBundle("~/bundles/others").Include(
        "~/Content/bootstrap/js/bootstrap.js", 
        "~/Scripts/jquery.dataTables.js", 
        "~/Scripts/dataTables_bootstrap.js", 
        "~/Scripts/bootstrap-dropdown.js", 
        "~/Scripts/jquery.dataTables.columnFilter.js", 
        "~/Scripts/Custom/CustomScript.js", 
        "~/Scripts/jquery.validationEngine-en.js", 
        "~/Scripts/jquery.validationEngine.js", 
        "~/Scripts/angular.js" 
        )); 

什么错我在干嘛?需要帮助...

+0

'loadCreateForm()'做了什么?你是用ajax调用加载的吗? –

+1

你正在注入你的模态html页面加载完成后,你的模态html没有绑定到$范围。你需要用'$ compile(html)($ scope)'来编译你的html,这样才能工作。 – Muctadir

回答

0

添加到@Muctadir所说的内容中,您需要告诉角度来检查角元素的模态部分。通过在将部分注入DOM之前调用$compile来完成此操作。问题是你必须能够访问$compile服务,并且你不能在你的loadCreateForm()里面这样做,你只能通过角度模块来做到这一点。所以,您需要以这样的方式组织代码,以便您可以获得所需的内容。这里是我的建议:

创建一个指示来处理你的模式和它的内容:

.directive('myModal', function($compile){ 
    return { 
     replace:false, 
     link: function(scope, element, attrs){ 
      var content = '@Url.Action("Create", "Requisition", new { area = "IMS" })'; 
      elem.load(content, null, function(){ 
       $compile(elem.contents())(scope); 
      }); 
     } 
    }; 
}); 

将加载内容,然后使用电流范围编制的内容,但内容已经从只加载后服务器。希望有所帮助。

+0

非常感谢。我遵循你的方式,它的工作。 – Void

+0

您可以发布完整的代码吗?我错过了一些东西,我有同样的问题,无法以任何方式工作。 – aly