0

I've found this answer here: https://stackoverflow.com/a/28357122/5459561

It works perfectly on Plunker, but when I implement it in my project, it doesn't work.

My HTML:

<div ng-controller="PolicyController as policy">
    <select multiple ng-model="proto" ng-init="proto=policy.protocols[0]"
    ng-options="protocol for protocol in policy.protocols"></select>
    <select multiple ng-model="fc" ng-options="function.code for function in
    policy.functions | filter:{protocol:proto}"></select>
</div>

My JS:

app.controller('PolicyController', ['$http', function($http){
    var policy = this;
    policy.functions = [{"code": 1, "protocol": "A"},{"code": 2, "protocol": "A"},{"code": 3, "protocol": "B"},{"code": 4, "protocol": "C"}];
    policy.protocols = ["A", "B", "C"];

Now the problem is when I click to change the current selected protocol, the codes disappear. But when I first initialize the page, I can see the codes for protocol A, which means the 'ng-init' is working properly.

NOTE: It does work in plunker as shown in the linked answer, but not in my project. I'm using angular V1.5.0, and the Plunker used 1.1.5. Though I don't believe it should make a difference.

Any ideas?

2
  • If it works in the plnkr, the mistake has to be somewhere else Commented Dec 28, 2015 at 9:44
  • Why do you have multiple on your selects? Do you really want to select multiple protocols, and multiple functions? If so, you'll have to use something else as a filter. Commented Dec 28, 2015 at 9:45

1 Answer 1

3

Your code works fine when you select only one protocol. But if you select multiple protocols, for example A and B, your $scope.proto variable contains the array [A,B] which messes up with your filtering.

The chosen answer on this question is the cleanest solution I can think of right now. Write a function that processes your first select's value array and returns true or false for second select based on that.

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

6 Comments

if you want to have multiple select (proto would be an array ) you have to also change your ng-init like this ng-init="proto=[policy.protocols[0]]" which again illustrates the problem you have as @sina-gh mentions.
You are right, the problem was that it was presented as an array. For some reason, it still worked here: plnkr.co/edit/uHDA7mrKoxgV0C2QgIN5?p=preview. What had to be done is to use | filter:{protocol:proto[0]} for single selected item.
Why would you define a multiple select list if you don't want to actually filter by multiple selections?
It was old code, I wrote it back when I thought that 'multiple' means to show multiple options instead of drop down select
Well, my solution is valid when you actually want to filter by multiple fields as you select them, for example "only show functions that have protocol A or B". If that was not what you intended, my solution should not be the chosen answer since a simple {protocol:proto[0]} or even better changing the select list to a single choice list would be much easier.
|

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.