2016-03-02 86 views
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 

这是纯粹的小说,还是有可能的?有没有其他解决方案来解决这类问题?

+0

好如果你要有条件地呈现你的“主模板”模板内你可以使用任何指令(我的首选解决方案),或使用NG-包括和URL设置为模板。并且可以使用ng-if或ng-switch属性设置条件。 –

回答

1

除了减少网络请求的数量,我没有看到任何真正的好处做你的问题中提到的内容。无论如何,您可能希望使用多个模态指令(errorModal,successModel等)以更好地划分您的代码。

如果你想减少网络请求,有一个$templateCache服务,使您可以用第一次请求预加载您的模板,在某种程度上是这样的:

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

您可能也想看看angular ui router这是一种更容易允许嵌套和主模板的替代路由器实现。

+0

是的,我已经使用了路由器和模板缓存。只是这样我才能写出更少的文件,就是这样 – Nick