Jahanzaib Aslam
Jahanzaib Aslam

Reputation: 2834

Error Invalid HTML for md-menu: Expected two children elements

The expected behaviour of md-menu is that we do the following:

<md-menu>
    <button ng-click="$mdOpenMenu()">Filters</button>
    <md-menu-content>
        <md-menu-item ng-repeat="field in devices.fieldList"><md-button ng-click="devices.setFilter(field)" ng-bind="field.name"></md-button></md-menu-item>
    </md-menu-content>
</md-menu>

However I have a situation when using md-menu-bar that I want a quick access button to refresh some data, this causes an error Invalid HTML for md-menu: Expected two children elements. Although I could perhaps look at a different UI it feels like you should be able to have the following functionality wise it works great:

Snippet

<md-menu-bar>
    <md-menu>
        <button ng-click="$mdOpenMenu()">Filters</button>
        <md-menu-content>
            <md-menu-item ng-repeat="field in devices.fieldList"><md-button ng-click="devices.setFilter(field)" ng-bind="field.name"></md-button></md-menu-item>
        </md-menu-content>
    </md-menu>
    <md-menu>
        <button ng-click="data.refresh()"><md-icon class="material-icons">refresh</md-icon></button>
    </md-menu>
</md-menu-bar>

Is there any reason this should not be done?

Upvotes: 3

Views: 2493

Answers (3)

Jeeva Arulanandam
Jeeva Arulanandam

Reputation: 141

if md-menu don't have children, use hide in md-menu-content to resolve the error

Home

<md-menu-content hide></md-menu-content>
</md-menu>

Upvotes: 1

Every md-menu must specify exactly two child elements https://material.angularjs.org/latest/api/directive/mdMenu

If you see your code, missing the second part of md-menu.

 <md-menu>
    <button ng-click="data.refresh()"><md-icon class="material-icons">refresh</md-icon></button>
</md-menu>

The second element is the md-menu-content element which represents the contents of the menu when it is open. Typically this will contain md-menu-items, but you can do custom content as well.

note: If you need a list of elements, try md-list:

<md-list>
   <md-list-item>
      <md-menu>
            <button ng-click="$mdOpenMenu()">Filters</button>
            <md-menu-content>
                <md-menu-item ng-repeat="field in devices.fieldList"><md-button ng-click="devices.setFilter(field)" ng-bind="field.name"></md-button></md-menu-item>
            </md-menu-content>
       </md-menu>
    </md-list-item>
    <md-list-item>
        <button ng-click="data.refresh()"><md-icon class="material-icons">refresh</md-icon></button>
    </md-list-item>
</md-list>

Upvotes: 4

kTn
kTn

Reputation: 595

for quick access button to refresh you can use it

<md-button ng-click="data.refresh()"><md-icon class="material-icons">refresh</md-icon></md-button>

check this link

Upvotes: 3

Related Questions