如何使用angularjs创建级联md-select?

时间:2021-01-02 19:40:51

I have JSON like this,

我有这样的JSON,

 $scope.analytics = {
                    'Twitter': [
                        'Following',
                        'Followers',
                        'Tweets'
                    ],
                    'Facebook': [
                        'People engaged',
                        'Likes',
                        'Clicks',
                        'Views'
                    ],
                    'LinkedIn': [
                        'Overview'

                    ]
                };

From the above, I need to create a cascading md-select , First md-select should have Twitter,Facebook and Linkedin.

从上面我需要创建一个级联md-select,首先md-select应该有Twitter,Facebook和Linkedin。

OnChanging Twitter, it should display the Following,Followers,Tweets in the next md-select.

在更改Twitter时,它应该在下一个md-select中显示以下,关注者,推文。

HTML:

<md-select ng-model="type" >
 <md-option ng-value="t" data-ng-repeat="t in analytics">{{ t }}</md-option>
 </md-select>
 <md-select ng-model="metrics" >
 <md-option ng-value="t" data-ng-repeat="t in analytics">{{ t }}</md-option>
</md-select>

App.JS:

app.controller('MainCtrl', function($scope) {
   $scope.analytics = {
                    'Twitter': [
                        'Following',
                        'Followers',
                        'Tweets'
                    ],
                    'Facebook': [
                        'People engaged',
                        'Likes',
                        'Clicks',
                        'Views'
                    ],
                    'LinkedIn': [
                        'Overview'

                    ]
                }; 
});

Here is my code in Cascading md-select

这是我在Cascading md-select中的代码

2 个解决方案

#1


The key to solving this is to place a watch on the value of the first select and control the options of the second. Also remember to clear the dependent selection, when the master selection changes!

解决这个问题的关键是观察第一个选择的值并控制第二个选项的选项。当主选择发生变化时,请记住清除相关选择!

A sample implementation:

示例实现:

Add a level2 variable in the scope. It will hold the options of the dependent select.

在范围中添加level2变量。它将保留从属选择的选项。

Place the watch on the first model:

将手表放在第一个型号上:

$scope.$watch('type', function(newval, oldval) {
  if( newval ) {
    $scope.level2 = $scope.analytics[newval];
  }
  else {
    $scope.level2 = [];
  }

  // delete the dependent selection, if the master changes
  if( newval !== oldval ) {
    $scope.metrics = null;
  }
});

Finally the markup needs a little tweaking to display correctly:

最后,标记需要稍微调整才能正确显示:

<md-select ng-model="type" >
    <md-option ng-value="k" data-ng-repeat="(k,v) in analytics">{{ k }}</md-option>
</md-select>
    <md-select ng-model="metrics" >
   <md-option ng-value="t" data-ng-repeat="t in level2">{{ t }}</md-option>
</md-select>

See a forked plunk: http://plnkr.co/edit/rI3AsC2plZ3w82WRBjAo?p=preview

看看分叉的插件:http://plnkr.co/edit/rI3AsC2plZ3w82WRBjAo?p = preview

#2


It may be simpler than that. Delete the $watch and just change the ng-repeat in the md-options item for metrics

它可能比那更简单。删除$ watch,只需更改md-options项中的ng-repeat以获取指标

<md-select ng-model="type">
    <md-option ng-value="k" data-ng-repeat="(k,v) in analytics">
        {{ k }}
    </md-option>
</md-select>
<md-select ng-model="metrics">
    <md-option ng-value="t" data-ng-repeat="t in analytics[type]">
        {{ t }}
    </md-option>
</md-select>

#1


The key to solving this is to place a watch on the value of the first select and control the options of the second. Also remember to clear the dependent selection, when the master selection changes!

解决这个问题的关键是观察第一个选择的值并控制第二个选项的选项。当主选择发生变化时,请记住清除相关选择!

A sample implementation:

示例实现:

Add a level2 variable in the scope. It will hold the options of the dependent select.

在范围中添加level2变量。它将保留从属选择的选项。

Place the watch on the first model:

将手表放在第一个型号上:

$scope.$watch('type', function(newval, oldval) {
  if( newval ) {
    $scope.level2 = $scope.analytics[newval];
  }
  else {
    $scope.level2 = [];
  }

  // delete the dependent selection, if the master changes
  if( newval !== oldval ) {
    $scope.metrics = null;
  }
});

Finally the markup needs a little tweaking to display correctly:

最后,标记需要稍微调整才能正确显示:

<md-select ng-model="type" >
    <md-option ng-value="k" data-ng-repeat="(k,v) in analytics">{{ k }}</md-option>
</md-select>
    <md-select ng-model="metrics" >
   <md-option ng-value="t" data-ng-repeat="t in level2">{{ t }}</md-option>
</md-select>

See a forked plunk: http://plnkr.co/edit/rI3AsC2plZ3w82WRBjAo?p=preview

看看分叉的插件:http://plnkr.co/edit/rI3AsC2plZ3w82WRBjAo?p = preview

#2


It may be simpler than that. Delete the $watch and just change the ng-repeat in the md-options item for metrics

它可能比那更简单。删除$ watch,只需更改md-options项中的ng-repeat以获取指标

<md-select ng-model="type">
    <md-option ng-value="k" data-ng-repeat="(k,v) in analytics">
        {{ k }}
    </md-option>
</md-select>
<md-select ng-model="metrics">
    <md-option ng-value="t" data-ng-repeat="t in analytics[type]">
        {{ t }}
    </md-option>
</md-select>