简体   繁体   English

角材料对话框选项卡式

[英]angular material dialog tabbed

I have two tabs on my dialog.我的对话框中有两个选项卡。 How do I dynamically set which one is active depending on which button the user hits.如何根据用户点击的按钮动态设置哪个处于活动状态。

Here are my buttons:这是我的按钮:

<md-button ng-click="showTabDialog($event, 'login')" class="md-raised md-primary">Login</md-button>

<md-button ng-click="showTabDialog($event, 'signup')" class="md-raised md-primary">Signup</md-button>

Here is my dialog html template这是我的对话框 html 模板

<md-dialog aria-label="Login">
<form>
    <md-toolbar>
        <div class="md-toolbar-tools">
            <h2>Login</h2>
            <span flex></span>
            <md-button class="md-icon-button" ng-click="cancel()">
                <md-icon aria-label="Close dialog"></md-icon>
                <!--md-svg-src="img/icons/ic_close_24px.svg"-->
            </md-button>
        </div>
    </md-toolbar>
    <md-dialog-content style="max-width:800px;max-height:810px; ">
        <md-tabs md-dynamic-height md-border-bottom>
            <md-tab label="Login">
                <md-content class="md-padding">
                    <h1 class="md-display-2">Log In</h1>
                    <p>Login here or click sign up to create a new account.</p>
                </md-content>
            </md-tab>
            <md-tab label="Sign Up">
                <md-content class="md-padding">
                    <h1 class="md-display-2">Sign Up</h1>
                    <p>Sign Up here</p>
                </md-content>
            </md-tab>
        </md-tabs>
    </md-dialog-content>
    <md-dialog-actions layout="row">
        <span flex></span>
        <md-button ng-click="answer('not useful')" >
            Not Useful
        </md-button>
        <md-button ng-click="answer('useful')" style="margin-right:20px;" >
            Useful
        </md-button>
    </md-dialog-actions>
</form>
</md-dialog>

Here is my JS:这是我的JS:

$scope.showTabDialog = function(ev, button) {
    var useFullScreen = ($mdMedia('sm') || $mdMedia('xs'))  &&       
$scope.customFullscreen;
    $mdDialog.show({
        controller: 'SignUpCTRL',
        templateUrl: 'app/login/signup.modal.html',
        parent: angular.element(document.h1),
        targetEvent: ev,
        clickOutsideToClose:true,
        openFrom: '#center',
        fullscreen: useFullScreen
    })
        .then(function(answer) {
            $scope.status = 'You said the information was "' + answer + '".';
        }, function() {
            $scope.status = 'You cancelled the dialog.';
        });
};

So to reiterate, the user hits the signup dialog and the box opens with the signup tab focused.所以重申一下,用户点击注册对话框,然后框打开,注册选项卡聚焦。 The user hits the login button and the dialog appears with the login tab focused.用户点击登录按钮,对话框出现并聚焦登录选项卡。

Edit: Better solution:编辑:更好的解决方案:

You can put "selected" property with a scope variable "selectedIndex" which will have wanted seleted tab value.您可以将“selected”属性与范围变量“selectedIndex”一起放置,该变量需要选择选项卡值。

<md-tabs selected="selectedTab" >

In corresponding controller:在相应的控制器中:

$scope.selectedTab=1;//tab number

You can then have a scope function changing its value using ng-click on your buttons to change this value;然后,您可以使用 ng-click 按钮更改此值,从而让范围函数更改其值;

Angular Material's built in md-tabs & md-tab directive has defaults that would need to be overwritten in order to set md-active by including your own ng-class . Angular Material 的内置 md-tabs & md-tab 指令具有默认值,需要覆盖这些默认值才能通过包含您自己的ng-class来设置 md-active 。 The Angular Material default ng-class on tabs looks like this:选项卡上的 Angular Material 默认 ng-class 如下所示:

ng-class="{ 'md-active':tab.isActive(), 'md-focused':tab.hasFocus(), 'md-disabled':tab.scope.disabled }"

Because my problem only had two tabs I disabled the default tab by capturing which button was pushed passing into locals then assigned it to a variable in my controller $scope.active and then disabled the default tab if the user didn't push login like so:因为我的问题只有两个选项卡,所以我通过捕获哪个按钮被推送到locals来禁用默认选项卡,然后将其分配给我的控制器$scope.active的变量,然后如果用户没有像这样推送登录,则禁用默认选项卡:

<md-tab label="Login" ng-disabled="active!='Login'">

I used two templates, two controllers, and a factory.我使用了两个模板、两个控制器和一个工厂。 The first template has the buttons that the user clicks:第一个模板具有用户单击的按钮:

<md-button ng-click="showTabDialog({tab: 0, event: $event})">One</md-button>

<md-button ng-click="showTabDialog({tab: 1, event: $event})">Two</md-button>

<md-button ng-click="showTabDialog({tab: 2, event: $event})">Three</md-button>

Note the zero-based counting.注意从零开始的计数。 The click passes through a data object with two items: which tab the user selected, and the $event (a MouseEvent ).单击通过包含两个项目的数据对象:用户选择的选项卡和$event (一个MouseEvent )。

The first controller includes this handler function:第一个控制器包括这个处理函数:

  $scope.showTabDialog = function(data) {
    var userData = {
      selectedTab: data.tab
    };
    userFactory.set(userData); // passes data object to other controllers
    $mdDialog.show({
      controller: DialogController,
      templateUrl: 'javascript/templates/tabDialog.html',
      parent: angular.element(document.body),
      targetEvent: data.event,
      clickOutsideToClose:true
    })
    .then(function(answer) {
      // fires on $scope.hide
    })
    .catch(function(error) {
      if (error) {
          console.error(error);
        }
      {
        // fires on $scope.cancel
      }
    });
  };

The handler function takes the data object passed in from the clicked button, then sends the data object to a factory that will pass the data object to the second controller.处理函数获取从单击按钮传入的data对象,然后将数据对象发送到工厂,工厂将数据对象传递给第二个控制器。 Next, $mdDialog.show is used to make the tabbed dialog, with parameters for the second controller and second template.接下来, $mdDialog.show用于制作选项卡式对话框,其中包含第二个控制器和第二个模板的参数。 The rest is boilerplate.其余的是样板。 targetEvent stops propagation up the DOM tree. targetEvent停止向上传播 DOM 树。 I put in then and catch in case you want to do something with $scope.hide and $scope.cancel .如果你想用$scope.hide$scope.cancel做点什么,我就输入then catch

Here's the factory:这是工厂:

app.factory('userFactory', function() {

  var userObject = null;

  function set(user) {
    userObject = user;
  };

  function get() {
    return userObject;
  };

  return {
    set: set,
    get: get
  };
});

I put the second controller in the first controller, below the handler function:我把第二个控制器放在第一个控制器中,在处理函数下面:


  function DialogController($scope, $mdDialog) {
    $scope.user = userFactory.get();
    $scope.selectedTab = $scope.user.selectedTab || 0;

    $scope.hide = function() {
      $mdDialog.hide();
    };

    $scope.cancel = function() {
      $mdDialog.cancel();
    };

    $scope.answer = function(answer) {
      $mdDialog.hide(answer);
    };
  }

This controller gets the data passed in through the factory, then sets the data to its $scope .该控制器获取通过工厂传入的数据,然后将数据设置为其$scope Note that this controller has its own $scope , not the $scope of the first controller.请注意,此控制器有自己的$scope ,而不是第一个控制器的$scope I don't use $scope.hide or $scope.answer but I left these in case you need them.我不使用$scope.hide$scope.answer但我留下了这些以防你需要它们。

And, finally, here's my template for the dialog:最后,这是我的对话框模板:

<md-dialog aria-label="tabbed dialog">
  <form>
    <md-toolbar>
    <div class="md-toolbar-tools">
      <img ng-src="myLogo.png" alt="logo" style="height: 36px;">
      <h2> MyCompany.com</h2>
      <span flex></span>
      <md-button class="md-icon-button" ng-click="cancel()">
        <md-icon md-svg-src="media/icons/ic_close_24px.svg" aria-label="Close dialog"></md-icon>
      </md-button>
    </div>
  </md-toolbar>

  <md-dialog-content style="max-width:800px;max-height:810px; ">
    <md-tabs md-dynamic-height md-border-bottom md-selected="selectedTab">
      <md-tab label="one">
        <md-content class="md-padding">
          <h1 class="md-display-2">Tab One</h1>
          <p>Lorem ipsum...</p>
        </md-content>
      </md-tab>

      <md-tab label="two">
        <md-content class="md-padding">
          <h1 class="md-display-2">Tab Two</h1>
          <p>Lorem ipsum...</p>
        </md-content>
      </md-tab>

      <md-tab label="three">
        <md-content class="md-padding">
          <h1 class="md-display-2">Tab Three</h1>
          <p>Lorem ipsum...</p>
        </md-content>
      </md-tab>
</md-tabs>
</md-dialog-content>

</form>
</md-dialog>

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

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