简体   繁体   English

如何在Angular JS中更改指令

[英]how to change directive in angular js

summary: click button >> page1 -> page2 摘要:单击按钮>>第1页->第2页

i try to make one page site. 我尝试制作一个页面站点。 so my main page is never refreshed but contents will be changed. 因此我的主页从未刷新,但是内容将被更改。
here is my main page 这是我的主页

<button id="click"></button>
<div id="main">
    <page1></page1>
</div>

all contents from directive page1. 指令page1中的所有内容。
but i want to change inner contents using exist directive called 'page2' (page1 -> page2) 但是我想使用称为'page2'(page1-> page2)的存在指令更改内部内容
so if i click button#click , div#main's contents will be changed 因此,如果我单击button#click,div#main的内容将被更改

.controller( 'MyCtrl', function MyCtrl( $scope ) {
    $scope.showTask =  function(){
      var main = angular.element(document.querySelector('#main'));


    };
})

.directive('page1', [function () {
  return {
      restrict: 'E',
      transclude: true,
      templateUrl: page1.html,
      replace: true
  };
}])

.directive('page2', [function () {
  return {
      restrict: 'E',
      transclude: true,
      templateUrl: page2.html,
      replace: true
  };
}])

but i can't find a solution how to change directive (page1 -> page2) please help me. 但我找不到解决方案如何更改指令(第1页->第2页),请帮助我。

If all you want is conditonnaly load page1.html or page2.html, you can use the ng-include directive. 如果只需要连续加载page1.html或page2.html,则可以使用ng-include指令。

See its documentation: https://docs.angularjs.org/api/ng/directive/ngInclude 查看其文档: https : //docs.angularjs.org/api/ng/directive/ngInclude

In your case, the html would be 在您的情况下,html将是

<button id="click" ng-click="selectPage()"></button>
<div id="main" ng-include="page"></div>

and your javascript 和你的JavaScript

$scope.selectPage = function() {
  // Decide which page to load here, for instance
  $scope.page = 'page1.html';
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM