0

这是一个快速的问题:

是否有可能拥有一个模板,其中您有多个模板并在需要时调用您需要的模板?

让我更好地解释一下:

我有一个我这样称呼的模态:

$scope.showErrorModal = ->
    errorModal = $ionicPopup.show(
      title: 'Issues list'
      scope: $scope
      templateUrl: './sections/modal/modal.tpl.html'
      buttons: [{text: 'Close',type: 'button-assertive'}
      ])
    errorModal.then (res) ->
      console.log 'tapped!', res
      return
    return

如您所见,我使用的是外部模板。

问题是,每次我的模态需要更改时,我都需要创建不同的模板。

我想做的(如果可能的话)是能够在内部创建各种子模板modal.tpl.html并以正确的模式调用它们。

这是一些示例代码:

modal.tpl.html:

<div id="error-template">
// here the error-modal stuff
</div>

<div id="success-template">
// here the success-modal stuff
</div>

并从控制器中,像这样调用它们,例如:

$scope.showErrorModal = ->
        errorModal = $ionicPopup.show(
          title: 'Issues list'
          scope: $scope
          templateUrl: './sections/modal/modal.tpl.html#error-template' //Just to make it clear that i want to use only one part of that file
          buttons: [{text: 'Close',type: 'button-assertive'}
          ])
        errorModal.then (res) ->
          console.log 'tapped!', res
          return
        return

这是纯粹的虚构,还是有可能?是否有其他解决方案可以解决此类问题?

4

1 回答 1

1

除了减少网络请求的数量之外,我认为做你在问题中提到的事情没有任何真正的好处。无论如何,您可能希望使用多个模式指令(errorModal、successModel 等)来更好地划分您的代码。

如果你想减少网络请求,有一个$templateCache服务可以让你在第一个请求时预加载你的模板,就像这样:

<script type="text/ng-template" id="templateId.html">
  <p>This is the content of the template</p>
</script>

您可能还想查看angular ui 路由器,它是一种替代路由器实现,更容易允许嵌套和主模板。

于 2016-03-02T17:03:10.230 回答