0

I've searched Google but can't find the answer...

I have an md-select that looks like this:

<md-input-container class="md-block" flex-gt-sm>
     <label>Type</label>
     <md-select ng-model="f.type" placeholder="Type" select-clear>
           <md-option ng-repeat="f in ctrl.type" value="{{f}}">{{f}}</md-option>
     </md-select>
</md-input-container>

ctrl.type looks like this in my controller:

this.type = ["type1","type2"];

and I am also sending the md-select value to an object array like this:

var data = {
    kind: $scope.f.type,
 };

and then I am doing an http.post that is sending the data object to an api.

Now here is my question. I would like it that when the md-select is empty, and the users sends the data object that instead of getting the error Cannot read property 'type' of undefined that simply the data is sent but it has a value of null instead of undefined, and then when the md-select is populated it simply sends the value to the api. How would I do that? Would I have to do form validation? Is there a simple way of doing this because I have about 10 of the md-selects that I have to send.

Thank you.

1
  • Based on the code snippets you have posted it looks like you are mixing "Controller as" syntax with $scope. I'm not an Angular expert, but is that even possible? Commented Mar 8, 2016 at 17:56

2 Answers 2

1

You can create a empty/dummy option above your md-option with something like

<option value='null'></option>
<option ng-repeat="f in type" value="{{f}}">{{f}}</option>

But I think, empty element is coming in the browswer because you are not initializing your f.type. If you properly initialize your f.type, you don't even get that empty element and its undefined value. You can initialize as shown below.

<select ng-model='f.type' ng-change='toggleSelection()' ng-init='f.type = type[0]'>
Sign up to request clarification or add additional context in comments.

Comments

0

Try to use ng-options instead of ng-repeat.

For example:

<md-select ng-model="f.type" 
    placeholder="Type" select-clear
    ng-options="f as f in ctrl.type">    
</md-select>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.