0

I am new to angular js and was playing around with it to try and get my code to return the flag of the country when the user types it in. So far my list filters down when they start typing a country but it wont display the flag. code:

 <h2> Section Two </h2>
<div ng-app="" ng-controller="ModuleTwoController">
    <p><input type="text" ng-model="test"></p>

    <ul>

        <li ng-repeat="x in filtered = (countries | filter:test)">
            {{ x.country }}
        </li>
    </ul>

    <p> Items in list: {{filtered.length}} </p>
    <div ng-switch on="countries">
        <div ng-switch-when="filtered.value === "Argentina">
            <img src="argentina.jpg" height="150" width="200">
        </div>
        <div ng-switch-when="USA">
            <img src="usa.jpg" height="150" width="200">
        </div>
        <div ng-switch-when="Brazil">
            <img src="brazil.jpg" height="150" width="200">
        </div>
        <div ng-switch-when="Hong Kong">
            <img src="hongkong.jpg" height="150" width="200">
        </div>
        <div ng-switch-when="UK">
            <img src="uk.jpg" height="150" width="200">
        </div>
        <div ng-switch-when="Turkey">
            <img src="turkey.jpg" height="150" width="200">
        </div>
        <div ng-switch-when="Rwanda">
            <img src="rwanda.jpg" height="150" width="200">
        </div>
        <div ng-switch-when="Federated States of Micronesia">
            <img src="fsom.jpg" height="150" width="200">
        </div>
        <div ng-switch-when="India">
            <img src="india.jpg" height="150" width="200">
        </div>
        <div ng-switch-when="South Africa">
            <img src="southafrica.jpg" height="150" width="200">
        </div>

what do i need to add to make it give me the results i want? I also only want the flag to appear when there is one country in the list. so for example if i type in 'u' usa, uk, turkey and south africa are all returned. but then if i type 'us' only the usa is returned. that is the only point i want a flag returned.

5
  • Please write a whole code in fiddler? Commented Jan 23, 2015 at 10:56
  • @sagar43 i have tried to put it in here. http://jsfiddle.net/6t5ne66n/2/ but for some reason it will not work online like it does in my browser... Commented Jan 23, 2015 at 11:15
  • Let me see. Please Wait Commented Jan 23, 2015 at 11:17
  • @RagingBull fiddler please do not add body tag. there is the option of adding url of external libs. also if you have any other local js please paste their contents in script portion. Commented Jan 23, 2015 at 11:23
  • new one here: http://jsfiddle.net/6t5ne66n/4/ still not working mind. Commented Jan 23, 2015 at 11:35

1 Answer 1

1

Here is your solution code Please Check

HTML

   <h2> Section Two </h2>
<div ng-app="myApp" ng-controller="ModuleTwoController">
    <p><input type="text" ng-model="test"></p>

    <ul>

        <li ng-repeat="x in filtered = (countries | filter:test)">
            {{ x.country }}
        </li>
    </ul>

    <p> Items in list: {{filtered.length}}  </p>
       <div ng-show="filtered.length == 1" >
    <div ng-switch on="filtered[0].country">
        <div ng-switch-when="Argentina">
            <img src="Argentina.jpg" height="150" width="200">
        </div>
        <div ng-switch-when="USA">
            <img src="usa.jpg" height="150" width="200">
        </div>
        <div ng-switch-when="Brazil">
            <img src="brazil.jpg" height="150" width="200">
        </div>
        <div ng-switch-when="Hong Kong">
            <img src="hongkong.jpg" height="150" width="200">
        </div>
        <div ng-switch-when="UK">
            <img src="uk.jpg" height="150" width="200">
        </div>
        <div ng-switch-when="Turkey">
            <img src="turkey.jpg" height="150" width="200">
        </div>
        <div ng-switch-when="Rwanda">
            <img src="rwanda.jpg" height="150" width="200">
        </div>
        <div ng-switch-when="Federated States of Micronesia">
            <img src="fsom.jpg" height="150" width="200">
        </div>
        <div ng-switch-when="India">
            <img src="india.jpg" height="150" width="200">
        </div>
        <div ng-switch-when="South Africa">
            <img src="southafrica.jpg" height="150" width="200">
        </div>
                <div ng-switch-default>
            Nothing
            </div>
        </div>

         </div>

            </div>

JsCode

var app = angular.module('myApp', []);

function ModuleTwoController($scope) {
   $scope.countries = [{country:'Argentina'},
                        {country:'USA'},
                        {country:'Brazil'},
                        {country:'Hong Kong'},
                        {country:'UK'},
                        {country:'Turkey'},
                        {country:'Rwanda'},
                        {country:'Federated States of Micronesia'},
                        {country:'India'},
                        {country:'South Africa'}
                ];
        }

and here is the working example JsFiddle

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

1 Comment

three things. firstly, thanks (almost perfect). secondly, so I was very close only missed one thing right? thirdly, the only thing to make it perfect would be having no flag appear until only one country left in list. so far the argentinian flag appears when nothing is typed in...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.