0

ng-model doesn't work like this : plunker link

I tried to alert ng-model="selectedFriend" and expect a pop-up of friend's name, but something is wrong when it's used within tab's content of angular bootstrap ui.

        <select ng-model="friendsModel" ng-options="friend.name for friend in myFriends.friends.data">
    <option value="">Select friend</option>
    </select>

I suspect it was $scope issue because it work fine here plunker link

2 Answers 2

2

Tabs are creating a new scope and thus you need to add a "famous dot" in your ng-model expression. Those scope-related issues are quite often and you can observe them even with built-in directives like ngInclude.

Finally, here is a working plunk: http://plnkr.co/edit/PPGA8SBwHeEQNAQCZT3K?p=preview

Sign up to request clarification or add additional context in comments.

4 Comments

very confusing, any other method?
especially this line $scope.friends = { model: undefined };
@user3522457 have a look at this comprehensive article to understand why this is needed and what are the alternatives: github.com/angular/angular.js/wiki/Understanding-Scopes
so tab became the parent's scope of selectedfriend since i placed them within tab?
0

You can even pass friendsModel as a parameter to selectFriend() method

<button ng-click="selectFriend(friendsModel)">select</button>

and in JS

$scope.selectFriend = function(friendsModel){
    console.log(friendsModel);
  };

2 Comments

so I still declare ng-model="selectFriend"?
Yes. You have to still declare.

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.