简体   繁体   English

使用指令时的角度重载控制器

[英]angular reloads controller when using a directive

My ui route has states like: 我的ui路线的状态如下:

        .state('projects', {
            abstract: true,
            url: "/?username&token",
            templateUrl: "projects/views/Projects.html",
            controller: 'ProjectController'
        })
       .state('projects.mine', {
            url: "mine",
            templateUrl: 'projects/views/ProjectsMine.html'
        })
        .state('projects.all', {
            url: "all",
            templateUrl: 'projects/views/ProjectsAll.html'
        })

I made a directive: 我做了一个指令:

    app.directive('description', function(){
        return{
            restrict: 'E',
            templateUrl : 'projects/views/directives/ProjectDescription.html',
            controller:ProjectController
        }
    });

I made two files exactly the same for the state templateUrls where the only difference is that the template of all uses the description directive like: 我为状态templateUrls制作了两个完全相同的文件,唯一的区别是所有模板都使用description指令,例如:

<description></description>

and the other one uses the content of the templateUrl for the description directive (copied 1on1) when not using the directive but the content of the templateUrl file the reload dosnt happen. 另一个则使用templateUrl的内容作为description指令(复制的1on1),而不使用该指令,而是发生重载的templateUrl文件的内容。

Everything works fine but when I go to the 'all' route the ProjectController gets reloaded. 一切正常,但是当我转到“全部”路线时,将重新加载ProjectController。 The directive looks like:" 该指令如下所示:“

<table>
  <tr >
      <td><b>name</b> </td>
      <td>{{project.name}}</td>
  </tr>
  <tr >
      <td><b>owner</b> </td>
      <td>{{project.owner}}</td>
  </tr>
<table>

the directive is added in an ng-repeat (in 'all' state) : 该指令以ng-repeat(处于“ all”状态)添加:

 <div ng-repeat="project in project|filter:searchText">
     <description></description>
 </div>

Links to states in my html: 链接到我的html中的状态:

    <a ui-sref=".mine()"><button>my projects</button></a>
    <a ui-sref=".all()"><button>all projects</button></a>

    <div ui-view></div>

the other one 'mine' uses the content of the directive and dosnt reload the controller. 另一个“我的”使用指令的内容,并且不重新加载控制器。 When going to 'all' from 'mine' a reload happens when I go from 'mine' to 'all' the controller isn't reloaded 从“我的”进入“全部”时,当我从“我的”变为“全部”时,会发生重载,控制器未重装

How can I keep everything as it is but avoid the reload of the controller? 如何保持一切正常,但避免重新加载控制器?

Removing 拆下

 controller:ProjectController 

fixed it, using controller:'controllerName' causes a controller to be created for every directive created. 使用controller:'controllerName'修复了该问题,将为每个创建的指令创建一个控制器。

您必须像函数调用一样链接到路由:

<a ui-sref=".list()" class="btn btn-primary">List</a> <a ui-sref=".paragraph()" class="btn btn-danger">Paragraph</a>

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

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