angular-ui / ui-select

AngularJS-native version of Select2 and Selectize
MIT License
3.26k stars 1.81k forks source link

tagging function with array of string not working #1990

Open Planet910 opened 7 years ago

Planet910 commented 7 years ago

I want to limit the tag creation for string with length < 2 but I always get this error:

Uncaught TypeError: Cannot create property 'isTag' on string

this is the tagging function:

vm.tagging` = function (text) {
            if(!text || text.length < 2)
                return null;
            return text;
        };

and here is a plunker: http://plnkr.co/edit/SS3V4A13nAbPDRAYrdaI?p=preview

You can see the error in the console and the new tag is not created. I suppose the error is because he expect an object to be returned from that function and I return a string. I dunno how to limit the creation of tag with an array of string and I really need this.

Jefiozie commented 7 years ago

Had a look at you plunker, i'm not getting the error or i'm doing something wrong. Can you verify if the plunker has the correct example?

Planet910 commented 7 years ago

I verified and I still get the error. Try to write "hello" inside the input and press Enter:

  1. The tag "hello" will not be created (in fact in the dropdown it does not appear) and the first element in the dropdown is added (in my case "Red")
  2. If you open the console (F12 in chrome, tab console) you'll see the error.
pmaoui commented 6 years ago

I had the same issue, here is how I made it works:

<ui-select multiple tagging="myScopeFunction" (...)

with:

myScopeFunction = function(newItem) {
    return {
       label: newItem,
       value: newItem,
    }
}

It will generate the correct values in the ngModel (array of strings).

mjy78 commented 6 years ago

Same issue here, but trying the approach suggested by @poupougnac doesn't work for me. The javascript error goes, but the value that is displayed in the dropdown list is the entire object.