Trying to change the material md-selected tab usin

2019-07-08 14:46发布

问题:

I'm trying to change the tab using code and can't seem to figure out the error. The basic thing works if we use the controller to change the variable but when I try to bind it through directive, it brokes.

var app = angular.module('MyApp', ['ngMaterial']);
app.controller('HelloCtrl', function($scope) {
  $scope.selectedTab = 0;
});
app.directive('something', function() {
  return {
    restrict: 'E',
    template: '<div ng-click="changeNavigation()">Change Navigation</div>',
    scope: {
      selectedTab: '='
    },
    controller: function($scope) {
      $scope.changeNavigation = function() {
        console.log('Hello World');
        $scope.selectedTab = 2;
      };
    }
  };
});

You can view the working code with error on codepen: http://codepen.io/piyushchauhan2011/pen/rVRqeV?editors=101

The error is:

回答1:

Change selectedTab="selectedTab" to selected-tab="selectedTab" while passing parameter to your directive.

See this working fiddle: http://jsfiddle.net/2yd9u7b9/