Tips for connecting to multiple items in an md-select element from within a directive

Looking to develop a straightforward directive that displays either a textbox or dropdown based on whether an array is provided for the model property on the scope.

Any value other than explicitly setting false in the directive markup such as multiple="false", will result in a multi-select dropdown.

Why is this approach not yielding the desired outcome? Additionally, it seems like the md-select value binding is not functioning (even though textbox binding is working), possibly due to the same issue.

Check out this Plunkr link showcasing the problem

End User

<div ng-app='home' layout-padding layout="row">
  <content-filter ng-repeat="filter in filters" flex="filter.width" model="filter.model" value="filter.value"></content-filter>
</div>

End User's Controller

app.controller('MainCtrl', function($scope) 
{
  $scope.filters = 
  [
    {
      model: 
      {
       multiSelect: false,
        items: 
        [
          {
            label: 'All',
            value: 'all'
          }, 
          {
            label: 'Fail',
            value: 'fail'
          }, 
          {
            label: 'Success',
            value: 'success'
          }
        ]
      },
      value: 'all',
      width: '50%'
    }, 
    {
      value: '123',
      width: '50%'
    }
  ];
 });

Custom Directive

app.directive('contentFilter', function() 
{
  return {
    restrict: 'E',
    replace: false,
    template: '\
      <md-input-container flex layout="fill" ng-if="model && model.items.length">\
        <md-select ng-model="value" multiple="model.multiSelect === true">\
         <md-option ng-repeat="item in model.items" ng-value="{{ item.value }}">{{ item.label }}</md-option>\
        </md-select>\
      </md-input-container>\
      <md-input-container flex layout="fill" ng-if="!model">\
        <input type="text" aria-label="{{ label }}" ng-model="value" />\
      </md-input-container>',
    scope: 
    {
      value: '=',      
      model: '=?'
    }
  };
});

Answer №1

It's possible that this isn't the solution you had in mind...

I've experimented with various ways to conditionally set multiple attributes for md-select, but none of them seem to be effective (ng-attr-multiple, ng-multiple...). It could potentially be a bug with angular-material.

As a temporary fix, you can consider adding two md-selects conditionally based on the attribute model.multiSelect value: one with the multiple attribute and the other without it. Here's an example:

<md-input-container flex layout="fill" ng-if="model && !model.multiSelect && model.items.length">\
    <md-select ng-model="value">\
     <md-option ng-repeat="item in model.items" value="{{ item.value }}">{{ item.label }}</md-option>\
    </md-select>\
</md-input-container>\
<md-input-container flex layout="fill" ng-if="model && model.multiSelect && model.items.length">\
    <md-select ng-model="[value]" multiple>\
     <md-option ng-repeat="item in model.items" value="{{ item.value }}">{{ item.label }}</md-option>\
    </md-select>\
</md-input-container>\

NOTE: When using md-select with multiple selections, the bound value must be an array. Therefore, you will need to change ng-model="value" to ng-model="[value]", as shown in the code above.

I have created a modified version of your plunker where you can see a functional example here

I hope this helps. Nonetheless, I remain open to additional solutions from others.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Challenges surrounding jQuery's .before

Currently, I am in the process of creating a simple carousel consisting of 4 divs. The carousel is utilizing 2 jQuery functions to position a div at either the first or last slot. The transitions being used are only alpha transitions as there is no need fo ...

Angular CSS Animation not functioning as expected

I have developed a system that retrieves stored messages from the server dynamically. The view for this system is structured as follows: <div id= "messages" data-ng-controller="MessageController"> <div class="message" data-ng-repeat="message ...

Having trouble with the "initSdk" property being undefined in your React Native Appsflyer integration?

I'm currently facing an issue while trying to integrate AppsFlyer into a react native application. The error I am encountering is "Cannot read property 'initSdk' of undefined" Initially, I imported the react-native-appsflyer module as shown ...

Having trouble launching an AngularJS website within a Docker container

I am currently working on running my angular site within a docker container using boot2docker. The structure of the Dockerfile is as follows: FROM ubuntu:14.04 RUN sudo apt-get update RUN sudo apt-get install -y npm # Setting the directory for commands ...

Can you explain the function of the "staticMoving" property in TrackballControls?

I was exploring the library module known as THREE.TrackballControls when I came across an interesting property within an instance of the module called staticMoving. This property appears to be connected to another property called dynamicDampingFactor. Howe ...

Puppeteer: Locating elements using HTML attributes

I'm currently working on getting Puppeteer to locate an element on this webpage using its attribute data-form-field-value, which needs to match 244103310504090. Here is the HTML code for the button in question: <section class="fl-accordion-tab--c ...

Can the sliding transition effect used in createMaterialTopTabNavigator be implemented in createMaterialBottomTabNavigator as well?

When using createMaterialTopTabNavigator, the transition from screen to screen involves a sliding effect as you press the tabs. On the other hand, createMaterialBottomTabNavigator uses a fading-in animation for its transitions. Is there a way to combine o ...

What is the process for transitioning from Ajax to Fetch API in JavaScript?

Currently, I am utilizing the JavaScript version of RiveScript which relies on ajax, and I have decided to move away from using jQuery. There is a single line of ajax code that I need to update to integrate the new Fetch API. **Note: The ajax code can be ...

Checking CORS permissions with a pre-flight OPTIONS request

During development, I implement a middleware called cors using the following syntax: app.use(cors({origin: 'http://localhost:8100'})); However, I have noticed that for every route request, two requests are being made as shown in the image below ...

Access the value of a submitted form using jQuery, when there are multiple forms with identical class names

I've looked for a solution here but couldn't find one that meets my needs. I have multiple forms with the class name .sbt-form: <form class='sbt-form'> <input name='kord' val=1/> </form> <form class= ...

Displaying the product quantity counter in the shopping cart immediately after adding the item

My website has a shopping cart quantity counter that doesn't update immediately when a product is added. Instead, it requires a page reload or navigation to another page for the change to be reflected. I would like the quantity counter to show the pro ...

Caution: Updating a component is not possible during the rendering of another component. ReactJS

I am encountering an error in my ReactHooks/Typescript application with a Navigation component that renders a PatientInfo component. The PatientInfo component is conditionally rendered based on the props it receives, determined by a searchbox in another ch ...

Generating a hyperlink to a specific user ID within a data table

I'm facing an issue with the formatting of my simple table when trying to navigate to user.id. Is there a better approach to this or should I consider moving LinkToUser? All styling has been removed to avoid any conflicts. import styled from 'st ...

Customize the appearance of every other column in an asp gridview

Looking for help with formatting rows and columns in an ASP GridView. The rows are automatically colored alternating, and I want to make the content in every first column bold and every second column normal. I have heard about using CSS nth-child() to achi ...

Transform a list separated by commas into an unordered list

Seeking a PHP, Jquery, or JavaScript method to convert comma-separated data into an unordered list. For clarification, I have uploaded a CSV file to WordPress where one section of content is separated by commas and I am looking to display it as a list. A ...

What could be causing the "no such file" error to occur while attempting to use the "mammoth" tool to convert a basic .docx file?

Here is my code snippet. The file contains a basic "hello world" and I have the hello.docx file located in the same directory where I am running this mammoth function. Error message: fatal Error: ENOENT: no such file or directory, open './hello.docx& ...

New React Component Successfully Imported but Fails to Render

I've encountered issues with the code I'm currently working on. Dependencies: React, Redux, Eslinter, PropTypes, BreadCrumb Within a view page, I am importing components from other files. The current structure is as follows: import Component ...

Retrieve and save files under a common directory

I have a specific route called payments/transactions that retrieves all relevant data. I'm interested in finding a way to generate a CSV file without the need to create new routes or add query parameters, so that my route appears as payments/transacti ...

How to trigger a file download instead of opening it in a new tab when clicking on a txt or png file in AngularJS

After retrieving my file URL from the backend API, I am trying to enable downloading when the user clicks a button. Currently, the download function works smoothly for Excel files (`.xlsx`), but for text (`.txt`) files or images (`.jpeg`, `.png`), it only ...

Utilize a function within an AngularJS directive

I'm struggling to understand how to pass a function (delegate) to a directive in AngularJS and utilize it within the link-function. Any guidance or suggestions on the correct approach would be immensely appreciated. Below is the controller code: myA ...