2

Currently learning Angular.js and to be honest struggling a lot with it... I am currently working on a simple set of dropdowns with the same options displayed. What I am trying to achieve is to ... let's say someone selects "Jane". I want it to remove "Jane" in the following dropdowns. I have the code set-up but the issue is that the options aren't showing up and nothing is really happening. I have the form in a file called index.html and the Javascript is in a file called app.js. The app is running locally with all the Angular files pulled in using npm not using a CDN.

I will display the code here on stack. Any help is appreciated and thank you in advance!

Here is the index.html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>MyApp</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<script src="app.js"></script>

</head>
<body>
<div ng:app>
<div ng-controller="HelloCntl">
<select ng-model="selectname1"
  ng-options="item as item.name for item in friends|filter:filter2|filter:filter3" ><option value="">- select -</option></select>
 <div>{{selectname1}}</div>

<select ng-model="selectname2"
    ng-options="item as item.name for item in friends|filter:filter1|filter:filter3" ><option value="">- select -</option></select>
 <div>{{selectname2}}</div>

<select ng-model="selectname3" ng-options="item as item.name for item in 
friends|filter:filter1|filter:filter2" ><option value="">- select -</option>
</select>
<div>{{selectname3}}</div>
</div>
</div>
</body>
</html>

and the app.js code:

function HelloCntl($scope) {
$scope.selectname1={};
$scope.selectname2={};
$scope.selectname3={};

$scope.filter1 = function(item){
  return (!($scope.selectname1&&$scope.selectname1.id)||item.id !=$scope.selectname1.id);
};

$scope.filter2 = function(item){
  return (!($scope.selectname2&&$scope.selectname2.id)||item.id!=$scope.selectname2.id);
};
$scope.filter3 = function(item){
  return (!($scope.selectname3&&$scope.selectname3.id)||item.id !=$scope.selectname3.id);
};
$scope.friends = [
  {
    id:1,name: 'John',
    phone: '555-1276'},
  {
    id:2,name: 'Mary',
    phone: '800-BIG-MARY'},
  {
    id:3,name: 'Mike',
    phone: '555-4321'},
  {
    id:4,name: 'Adam',
    phone: '555-5678'},
  {
    id:5,name: 'Julie',
    phone: '555-8765'}

];

}

I have seen this example work in other demos but for me it just doesn't seem to be working... very curious about possible solutions. Thanks again!

6
  • I've made a fiddle following your code, and it works well as your purpose. Please check this fiddle. jsfiddle.net/Canet/6fe2z0my/1 Commented Nov 29, 2017 at 0:44
  • I dont know if I'm putting the code in the wrong files or what but it just won't work with what I have locally... ? I really appreciate you doing that but do you have any idea what's causing it not to work.. ? Commented Nov 29, 2017 at 0:59
  • Is that an entire code of 'app.js'? I'm using angular1 and also I've been learning angular in a short period of time. So I wonder if 'ng:app' works well. You can check what I've declared in my fiddle like 'ng-app'. Commented Nov 29, 2017 at 1:06
  • I think the issue is that I'm using Angular 2... is there a way to use a CDN to bring in the Angular 1 files? I want to see if it will work then. Commented Nov 29, 2017 at 1:09
  • I got it working using a Angular 1 CDN and it works. Thanks so much for your help! Can you make it an official answer so I can accept it as an answer? Commented Nov 29, 2017 at 1:26

2 Answers 2

1

I've made a fiddle based on your code using Angular1.

<div ng-app="MyApp">

I would like to recommend using 'ng-app' instead of 'ng:app' if you can use Angular1.

You can see your code works well by referring to my fiddle. :)

And can get more information about 'ng-app' by this link. (document of angularjs-ngApp directive)

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

Comments

0

You forgot to load AngularJS in your HTML. Put this just below the favicon <link>:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>

Completely forgivable, it took me an embarrassing amount of time to spot the mistake myself!

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.