更改 AngularJS md-tabs 的索引根本没有效果 [英] Change index of AngularJS md-tabs have no effect at all

查看:22
本文介绍了更改 AngularJS md-tabs 的索引根本没有效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Angular 应用程序中,我有一个 md-tabs,它的 md-selected 指令绑定到我的控制器中的一个属性.我想将当前标签更改为索引由 ng-click 调用的函数设置的标签.

我是这样做的:

<md-content ng-if="isSmart" 布局填充><md-tabs md-selected="selectedIndex" 布局填充><md-tab>.........</md-tab><md-tab>.........</md-tab><md-tab>.........</md-tab><md-tab><md-tab-label>{{ 'tracking.positions.TITLE' |翻译 }}</md-tab-label><md-tab-body><md-tab-content layout-fill flex><button ng-click="map.panTo(getPosition());displayMap();"></button></md-tab-body></md-tab></md-tabs></md-content>

在我的控制器中,我有:

 $scope.selectedIndex = 0;$scope.displayMap = function() {$scope.selectedIndex = 1;};

但是当我点击调用 displayMap() 的按钮时它根本没有效果;

我已经检查了问题:

  • 当我设置 $scope.selectedIndex = 1;在我的控制器中,默认选项卡是索引为 1 的选项卡.确定
  • 当我在模板中设置 md-selected="1" 时,默认标签是索引为 1 的标签.确定
  • 当我在代码中设置断点并单击按钮时,将调用 displayMap(),并且 $scope.selectedIndex = 1;被执行.好的

似乎一切正常……除了标签没有改变.

我正在运行 Angular Material 1.0.2

我什至用 $apply 强制更新(无效):

 $scope.selectedIndex = 0;$scope.displayMap = 函数 () {$超时(函数(){如果 (!$scope.$$phase) {$scope.$apply(function () {$scope.selectedIndex = 1;});}});};

解决方案

我解决了我的问题,这肯定是由范围问题引起的.我只是简单地使用了 controller as 语法,并声明了每个以前的范围数据:

var self = this;self.selectedIndex = 0;self.displayMap = function (){self.selectedIndex = 1;};

和我的标记:

<md-content ng-if="tracking.isSmart" 布局填充><md-tabs md-selected="tracking.selectedIndex" 布局填充><md-tab>.........</md-tab><md-tab>.........</md-tab><md-tab>.........</md-tab><md-tab><md-tab-label>{{ 'tracking.positions.TITLE' |翻译 }}</md-tab-label><md-tab-body><md-tab-content layout-fill flex><button ng-click="tracking.displayMap();"></button></md-tab-content></md-tab-body></md-tab></md-tabs></md-content>

现在工作完美.我猜我的 ng-if 正在修改我的范围.

In my Angular app, I have a md-tabs whose md-selected directive is binded to a property in my controller. I'd like to change the current tab to the one whose index is set by a function called by ng-click somewhere else in my template.

I did it this way:

<div ng-controller="TrackingCtrl" layout-fill>
   <md-content ng-if="isSmart" layout-fill>
      <md-tabs md-selected="selectedIndex" layout-fill>
         <md-tab>.........</md-tab>
         <md-tab>.........</md-tab>
         <md-tab>.........</md-tab>
         <md-tab>
            <md-tab-label>{{ 'tracking.positions.TITLE' | translate }}</md-tab-label>
            <md-tab-body>
                <md-tab-content layout-fill flex>
                    <button ng-click="map.panTo(getPosition());displayMap();"></button>
            </md-tab-body>
         </md-tab>
    </md-tabs>
 </md-content>
</div>

In my controller I have :

  $scope.selectedIndex = 0;
  $scope.displayMap = function() {
      $scope.selectedIndex = 1;
  };

But it has no effect at all when I click my button which calls displayMap();

I've inspected the problem:

  • When I set $scope.selectedIndex = 1; in my controller, the default tab is the one whose index is 1. OK
  • When I set md-selected="1" in my template, the default tab is the one whose index is 1. OK
  • When I set a breakpoint in my code, and when I click my button, displayMap() is called, and $scope.selectedIndex = 1; is executed. OK

It seems everything works fine... except the tab doesn't change.

I'm running Angular Material 1.0.2

I even used $apply to force update (no effect) :

  $scope.selectedIndex = 0;
  $scope.displayMap = function () {
      $timeout(function () {
          if (!$scope.$$phase) {
              $scope.$apply(function () {
                  $scope.selectedIndex = 1;
              });
          }
      });
  };

解决方案

I solved my problem which was certainly caused by a scope issue. I simply used the controller as syntax, and declared every previous scope data with:

var self = this;
self.selectedIndex = 0;
self.displayMap = function (){
   self.selectedIndex = 1;
};

and my markup:

<div ng-controller="TrackingCtrl as tracking" layout-fill>
  <md-content ng-if="tracking.isSmart" layout-fill>
    <md-tabs md-selected="tracking.selectedIndex" layout-fill>
       <md-tab>.........</md-tab>
       <md-tab>.........</md-tab>
       <md-tab>.........</md-tab>
       <md-tab>
          <md-tab-label>{{ 'tracking.positions.TITLE' | translate }}</md-tab-label>
          <md-tab-body>
             <md-tab-content layout-fill flex>
                <button ng-click="tracking.displayMap();"></button>
             </md-tab-content>
          </md-tab-body>
       </md-tab>
  </md-tabs>
 </md-content>
</div>

Works perfect now. I guess my ng-if was modifying my scope or so.

这篇关于更改 AngularJS md-tabs 的索引根本没有效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆