2

i don't understand what my ng-show not working when i click on my button with ng-click... thanks for help.

<div ng-show="showMe == 1">
     <h5>Ajouter</h5>
     <input type="texte">
</div>
<table>
    <thead>
        <tr>
            <th>Numéro :</th>
            <th>Type de Produit :</th>
        </tr>
    </thead>
    <tbody ng-repeat="product in shopCtrl.tableProduct">
        <tr>
            <td>{{product.id}}</td>
            <td>{{product.name}}</td>
            <td class="text-right">
                <div>
                    <button ng-click="showMe = 1">Ajouter</button>
                </div>
            </td>
    </tbody>
</table>

3 Answers 3

1

The answer of gtlambert is true. However if you have more than one level of ng-repeat or another directive that does the same thing you'll have trouble.

To not have any trouble use objects like this :

$scope.params = {showMe:0};// init in controller

<div ng-show="params.showMe == 1">
<button ng-click="params.showMe = 1">

This will always works whatever the number of ng-repeat/directive you use.

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

Comments

0

When you use ng-repeat this creates a new scope. To access the main controller scope from inside the ng-repeat you need to use $parent.

So, change your ng-click to $parent.showMe = 1 and this will fix the problem:

<button ng-click="$parent.showMe = 1">Ajouter</button>

Comments

0

Here you go. I have a working example. showMe becomes a member of your controller.

function ShopController() {
  this.showMe = false;
  this.tableProduct = [{
    id: 1,
    name: "Bar"
  }];
}

angular.module('app', [])
  .controller('ShopController', ShopController);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ShopController as shopCtrl">
  <div ng-show="shopCtrl.showMe">
    <h5>Ajouter</h5>
    <input type="texte">
  </div>
  <table>
    <thead>
      <tr>
        <th>Numéro :</th>
        <th>Type de Produit :</th>
      </tr>
    </thead>
    <tbody ng-repeat="product in shopCtrl.tableProduct">
      <tr>
        <td>{{product.id}}</td>
        <td>{{product.name}}</td>
        <td class="text-right">
          <div>
            <button ng-click="shopCtrl.showMe = true">Ajouter</button>
          </div>
        </td>
    </tbody>
  </table>
</div>

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.