3

hi i'm trying to create $scope dynamically and set it true on click to show some details inside ng-repeat but it doesn't seem to work ,Here is the plunk plunk

var app=angular.module('myApp',[]);
 app.controller('myCtrl', function($scope){
 $scope.details=[{
 cid:100,
  foo:'first component'
  },

 {
   cid:101,
 foo:'second component'
  },{
   cid:102,
    foo:'third component'
    }];
 $scope.showGraph=function(serverid){
  console.log(serverid);
   if ($scope[serverid] === undefined) {

     $scope[serverid] = true;
     console.log($scope[serverid]);
    } else {
    $scope[serverid] = true;
     }
      };
   });

thanks for your help.Any other suggestions to achieve this is also welcomed.

1
  • 1
    Your ng-if shows fundamental javascript misunderstanding of dynamic variable names ... String + variable returns string not variable Commented Jun 5, 2016 at 12:42

1 Answer 1

3

Currently you are generating some variable name by dynamically passing serverid parameter to it(single scope variable is taking responsibility of all details collection). And the main issue was how you were trying to retrieve variable value from scope isn't correct syntax(ng-if directive expression trying to concatenate strings).

The simplest way to achieve this would be, have a flag showGraph on each element of details collection. Then on click just toggle showGraph flag

  <ul ng-repeat='detail in details'>
    <li><p>{{detail.foo}} <a href="#" ng-click='detail.showGraph =!detail.showGraph'>show graph</a>
    </p>
    <div ng-if='detail.showGraph'>
      show graph here
    </div>
    </li>
  </ul>

Forked Plunkr

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

1 Comment

my app is different from that plunker.i'm getting a response from api.

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.