material icon indicating copy to clipboard operation
material copied to clipboard

md-select error on name attribute

Open hodeyp opened this issue 11 years ago • 9 comments

I need to define the name on an md-select programatically using {{id}}. However, when doing so an error is thrown...

TypeError: Cannot read property '$name' of undefined

the offending code is on line 8029 of angular-material.js...

if (attr.name && formCtrl) {
var selectEl = element.parent()[0].querySelector('select[name=".' + attr.name + '"]')
formCtrl.$removeControl(angular.element(selectEl).controller());
}

it looks to be a timing issue of when the attr.name is bound, as a hack I put the function into a $timeout and it worked correctly.

hodeyp avatar Jun 01 '15 12:06 hodeyp

I am having the same issue. I am defining the name of an mdSelect directive using the following code:

<md-select name="type{{$index+1}}"

I am getting: TypeError: Cannot read property '$name' of undefined

FYI: This is part of an ng-repeat loop.

evoker avatar Jun 04 '15 15:06 evoker

Any word on this? For someone that cannot hack angular-material.js, this is a serious issue...

evoker avatar Jun 05 '15 09:06 evoker

Same issue here (version1.4.0) even when I don't set it programatically, I can just leave it without 'name' for now but then I get no validation.

<md-input-container>
    <md-select name="addressCountry" placeholder="Country" ng-model="company.addressCountry" required>
         <md-option ng-value="country" ng-repeat="country in countries">{{ country }}</md-option>
    </md-select>
    <div class="errors" ng-messages="companyForm.addressCountry.$error" ng-show="companyForm.addressCountry.$touched && companyForm.addressCountry.$invalid">
        <div ng-message="required">Country required</div>
    </div>
</md-input-container>

I don't think there is anything wrong with my code.

apat183 avatar Jun 07 '15 15:06 apat183

Have same problem, +1

it happens here:

if (attr.name && formCtrl) {
  var selectEl = element.parent()[0].querySelector('select[name=".' + attr.name + '"]')
  formCtrl.$removeControl(angular.element(selectEl).controller()); // <--- controller() returns undefined
}

https://github.com/angular/material/commit/aa440efcb381a61abb49ea846d1fe0b458cc3a50#diff-d7b463f733c2e3fbc783f38073561b74R153

Intention is unclear to me. If I step through the code in debugger, hidden select element is not part of the form anyway. If I comment this block, then it works for me. I didn't tried $timeout solution suggested by @hodeyp (can you provide the code pls?). It has to resolve #1801 somehow, but I do not use it so cannot test.

ghost avatar Jun 08 '15 12:06 ghost

+1 have the same issue. It's working in my application, but it's failing with this issue in my integration tests.

iamisti avatar Jun 23 '15 08:06 iamisti

+1 @evoker I had this issue when using with ng-repeat too.

I found a workaround: http://stackoverflow.com/questions/14378401/dynamic-validation-and-name-in-a-form-with-angularjs

which uses outer form + inner form for each ng-repeat item

molog avatar Jun 25 '15 09:06 molog

+1 same issue, hope this one get fixed soon.

wenbolovesnz avatar Jun 26 '15 05:06 wenbolovesnz

+1

eXaminator avatar Jun 27 '15 18:06 eXaminator

This appears to be caused by lack of a controller around the directive.

http://codepen.io/karlhorky/pen/OVZOqy

I've opened a PR: angular/material#3671

karlhorky avatar Jul 10 '15 10:07 karlhorky