单击角度 - 更新指令模板

mar*_*osh 5 angularjs angularjs-directive

我正在尝试使用Angular构建页面.

我说有两个模板

<script type='text/ng-template' id='a.html'>
    /*some html using {{data}} */
    <div><button>A</button></div>
</script>
Run Code Online (Sandbox Code Playgroud)

<script type='text/ng-template' id='b.html'>
    /*some html using {{data}} */
    <div><button>B</button></div>
</script>
Run Code Online (Sandbox Code Playgroud)

我想创建一个指令,当单击模板内的按钮时,它将模板从a更改为b,反之亦然.我尝试了几个选项,查看类似问题的其他答案,但没有任何运气.你能帮我找到最简单的方法吗?

poc*_*sar 10

ng-include是一个漂亮的指令,接受范围变量作为参数.这样,您就可以动态加载(包括加载模板中的其他指令).请参阅plnkr http://plnkr.co/edit/qxBVDWx6P0F0aNuOymct?p=preview

app.directive('dynamicTemplate', function(){
  return {
    restrict: 'A',
    replace: true,
    template: '<div><div ng-include="var"></div><div>current var: {{ var }}</div></div>',
    controller: function($scope){
      $scope.var = 'template1.html';
      $scope.change = function(where){
        $scope.var = where;
      }
    }
  };
});
Run Code Online (Sandbox Code Playgroud)

但最好的方法就是处理使用$stateProvider$routeProvider