0

I currently have two arrays of objects made in my javascript. The first array named list1 has keys named show and number, a boolean and integer. The second array named list2 has a key named number that takes on an integer.

I currently have a button that will add a number from list1 to list2, and I want to disable it the number already exists in list2.

Here is the HTML only showing repeat (my controller is renamed to list)

HTML

<li ng-repeat="item in list.list1>
   <a href="#" 
      class="btn btn-info btn-large" 
      ng-disabled=!"item.show" 
      ng-click="list.addItem(id, $index)">

      Add
   </a>
</li>

JS

this.addItem = function(id, index)
{
   this.list1[index].show = false;

   this.list2.push({number: id})
}; 

I'm not sure why my code isn't working and this isn't the first implementation I've tried. Would really appreciate some tips on how to do this correctly. Thanks!

Sorry about the bad initial code, sleepless night hasn't really helped me debug this.

9
  • What is the index in list1[index]? Commented Jul 29, 2014 at 18:00
  • Forgot to include $index in the function, updated code to reflect changes Commented Jul 29, 2014 at 18:04
  • Is list the alias for your controller? And what is pmt? Commented Jul 29, 2014 at 18:08
  • I think what you have posted now should work except for the incorrect placement of ! in the ng-disable attribute Commented Jul 29, 2014 at 18:13
  • 1
    I recommend putting this up on plunkr or jsfiddle and posting a link. You will get much faster and better help that way. Commented Jul 29, 2014 at 18:17

1 Answer 1

1

From your code for addItem, it looks like each entry in list1 has a property named show, rather than the array having a property named show. If that's the case, then your anchor should look something like this:

<a href="#" class="btn btn-info btn-large" ng-disabled="!pmt.show" ng-click="list.addItem(id, $index)">Add</a>

Also, the ! operator should be within the quotes on ng-disabled.

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

5 Comments

+1, although I think there may be other problems. E.g. what is list.pmt? Is addItem in a service, and is the repeat binding to the correct scope?
Oh yeah, I tried that before but I still couldn't get it working, that's definitely what I meant to write in my question
I think your right @PhilSandler, looks like there are multiple issues here and the posted code doesn't give us enough to really troubleshoot this
Sorry updated code, had trouble sleeping last night and it's taking a toll on my ability to focus
No problem @user3821516, we've all had those days :) And welcome to StackOverflow!

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.