简体   繁体   English

单击angular ui选项卡会多次触发事件

[英]Clicking on angular ui tabs fires event many times

I have 15 tabs using angular ui tabs like this 我有15个标签使用像这样的角度ui标签

This is template 这是模板

<tabset justified="true">
    <tab heading="{{ tab.display }}"
         select="tsc.selectTab(tab.date)"
         ng-repeat="tab in tsc.tabs">
         <div ng-include="'entries.html'"></div>
    </tab>
</tabset>

This is controller 这是控制器

$scope.activeTabDate = '';

self.selectTab = function (tabDate) {
    $scope.activeTabDate = tabDate;

};

Now This is my child controller for entries.html 现在这是entries.html的我的子控制器

$scope.$parent.$watch('activeTabDate', function (newValue, oldValue) {
    if (newValue !== oldValue) {
        console.log('--'+newValue);
    }
});

I have 15 Tabs on the Page. 我在页面上有15个标签。 My problem is everytime i click on tab . 我的问题是每次我点击标签。 In console.log i get 15 entries instead of one. 在console.log中,我得到15个条目而不是一个条目。 Why is that 这是为什么

Remove the manual import from your entries.html and use ng-controller in the div that includes entries.html. 从entries.html中删除手动导入,并在包含entries.html的div中使用ng-controller。 Then, I think you will not need to use $scope.parent in the child controller, as the scope will be the same as the main one 然后,我认为你不需要在子控制器中使用$ scope.parent,因为范围将与主要的一样

<tabset justified="true">
    <tab heading="{{ tab.display }}"
         select="tsc.selectTab(tab.date)"
         ng-repeat="tab in tsc.tabs">
         <div ng-include="'entries.html'" ng-controller="yourchildcontroller"></div>
    </tab>
</tabset>


$scope.$watch('activeTabDate', function (newValue, oldValue) {
    if (newValue !== oldValue) {
        console.log('--'+newValue);
    }
});

EDIT You are executing the watch from each tab controller anyway also with my first change. 编辑您也可以通过我的第一次更改从每个标签控制器执行手表。

This way the controller will control all the child elements of the tabset and share the same $scope. 这样控制器将控制tabset的所有子元素并共享相同的$ scope。

<tabset justified="true" ng-controller="yourchildcontroller">
<tab heading="{{ tab.display }}"
select="tsc.selectTab(tab.date)"
ng-repeat="tab in tsc.tabs">
<div ng-include="'entries.html'" ></div>
</tab>
</tabset>


$scope.$watch('activeTabDate', function (newValue, oldValue) {
  if (newValue !== oldValue) {
    console.log('--'+newValue);
  }
});

https://docs.angularjs.org/api/ng/directive/ngController https://docs.angularjs.org/api/ng/directive/ngController

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

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