Martel Benjamin
Martel Benjamin

Reputation: 150

Questions on ngshow and scope

I have two question on angularJS, i use differents sections and i want to show just one so i did : <section ng-show="section6_us"> </section> and <section ng-show="section7_us"> </section> for exemple and i have a lot of variable in my scope.

Can we do the thing like this ? <section ng-show="{section6_us == "section6"}"> </section> and <section ng-show="{section7_us == "section7"}"> </section> With a variable and a compare ? it would be great because i can't use ng-switch and ng-if and i have a lots of variable like section1,section2,...section 14 etc.

Second, it's possible that we can use the scope like this ? $scope.("section"+i) = true for example ?

Thank you in advance Ben

Upvotes: 2

Views: 45

Answers (2)

Kop4lyf
Kop4lyf

Reputation: 4590

I am not fully sure what you are trying to say here but as per my understanding:

It should be differnt quotes.

Note that I have used single to denote value

<section ng-show="{section7_us == 'section7'}"> </section>

Plus to store dynamic properties , you can use square brackets: like:

$scope["section"+i] = true;

This is no angular, just plain javascript objects property addition.

Upvotes: 1

Dominic Scanlan
Dominic Scanlan

Reputation: 1009

Yes to ng-show="{section7_us == "section7"}">

and i think you'll want to look at $parse for dynamic scope variables.

var the_string = 'life.meaning';

// Get the model
var model = $parse(the_string);

// Assigns a value to it
model.assign($scope, 42);

// Apply it to the scope
$scope.$apply();

Original link here

Upvotes: 0

Related Questions