加载 Angular 的 UI 引导模式与动态内容

Load Angular's UI Bootstrap modal with dynamic content

本文关键字:动态 模式 Angular UI 加载      更新时间:2023-09-26

我正在尝试使用Angular和UI Bootstrap使模态动态加载模板,但我什至不知道该怎么做。

这是我到目前为止的代码:

小提琴:https://jsfiddle.net/fwpcsfg7/8/

我想要的是拥有以下ng-repeat

<div ng-app="MyApp" ng-controller="ExampleController as example">
  <!-- load a different template (partial) inside the modal depending on the value of item.type -->
  <div class="someClass" ng-repeat="item in example.items" ng-click="example.loadModal(item.templateName)">Modal {{item.type}}</div>
</div>
<!-- The modal that would change it's content depending on the item type-->
<div class="modal" ng-controller="ModalController as modal" ng-include src="example.modalTemplate+'.html'"></div>
<!-- Defining Angular cached partials -->
<script type="text/ng-template" id="modalType1.html">
  <input ng-model="item.name"></input>
</script>
<script type="text/ng-template" id="modalType2.html">
  <input ng-model="item.name"></input>
  <input ng-model="item.age"></input>
</script>
<script type="text/ng-template" id="modalType3.html">
  <input ng-model="item.name"></input>
  <input ng-model="item.name"></input>
</script>
<script type="text/ng-template" id="modalType4.html">
  <input ng-model="item.name"></input>
  <input ng-model="item.lastName"></input>
  <input ng-model="item.country"></input>
  <input ng-model="item.location"></input>
</script>

我想为每个item.type值显示一个具有不同内容的模态。如果你能给我一个基本的小提琴,我会很高兴的。

现实生活中的情况更复杂,所以我需要将模态的内容加载为模板(也许为每个item.type制作模态更容易?

编辑:我不知道如何解决的问题之一是:如果我在其他完全不同的控制器上,如何访问item属性?

如何在使用 angular js 单击时将 html 模板附加到div/指令可能会有所帮助?

有一些 X 变量来指示要加载的模板。并将 ng-include 与 src="X" 一起使用