0

I have a button:

 <button  data-ng-click="toggleElement(asset)"   class="btn"><span class="text-center">Add To Cart</span></button>

I would like to hide this button when this element will add in a list and show
a disable button with the title "Added to Cart"!!

I tried it :

<table class="table" data-ng-show="elements!=null && elements.length>0">
  <tr  data-ng-repeat="element in elements">          
            <td> 
            <button  data-ng-click="toggleElements(element)"  ng-disabled="isDisabled" ng-model="isDisabled"  class="btn"><span class="text-center">Add To Cart</span></button>
    <td> <button data-ng-click="toggleAsset(elements[$index])" data-ng-disabled="added" class="btn">{{added ? 'Added' : 'Add'}}</button>             
</td> 
 </tr>

    </table>

in my controller I have this :

$scope.toggleElements= function (element){
        .....
           $scope.added  = true;
}

Somebody can help me...

1
  • What have you tried? Angular documentation should be more than enough to answer this question with the added bonus of learning the framework. Commented Jan 8, 2014 at 17:55

3 Answers 3

2

I think this will work for you:

<button ng-click="toggleElement(asset)" ng-disabled="asset.added">{{asset.added ? 'Added' : 'Add'}}</button>

You'll need to set the property asset.added inside the toggleElement() method.

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

2 Comments

I have asset.added undefined...How can I add it!!! It is possible that I have to put ng-model="assets[$index].added" ??
Remember that if you are inside a ng-repeat you don't need to use the $index variable. You can just grab the current item being iterated and use it.
0

You can execute several instructions inside one ng-click directive. Just separate them with ; like a normal javascript expression.

For example:

<button ng-click="doIt(); hide = true" ng-hide="hide">
  click me!
</button>

6 Comments

The problem with this approach is that the hide variable is assigned to the current scope. If you are outside a ng-repeat or other directive, which create isolated scopes, you can leak this variable to other elements. This is also complicate unit testing, you are putting part of the logic on the view...
This will define a variable $scope.hide which will be shared by all assets, so if it's used in a list, it will hide all buttons, not just one. While not clear from the OP's question, I think it's safe to assume that there's a a list of assets
@mnemosyn disagree with you. It won't hide all buttons. Check my plunkr.
@gab: true, but that's because ng-repeat creates an isolated scope. If you simply copy the button, they'll interact. William Lepinski's comment already points that out.
@user880386: use William Lepinski's approach where the hidden property is stored on the object rather than the scope.
|
0

here is one solution.

Have a look to the plunk

<button ng-repeat="a in [0,1,2]" data-ng-click="added = !added" class="btn" ng-disabled="added">
  <span ng-show="!added" class="text-center">Add To Cart</span>
  <span ng-show="added" class="text-center">Added To Cart</span>
</button>

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.