m1st m1st - 4 months ago 10
AngularJS Question

How to code optional attribute without value to show/hide some blocks?

How to code optional attribute without value to show/hide some blocks?

Demo

For example, when the "showsum" attribute exists in the line below:

<div ng-controller="myCtrl" showsum headers="['Table Header 1', 'Table Header 2']">


I want to show this line (ex: Sum: 12)

<td ng-show="showsum">Sum: {{ getCol1Sum() }}</td>

Answer

well since the ng-show directive takes in an expression, you cannot use it the way you did there,

The ngShow directive shows or hides the given HTML element based on the expression provided to the ngShow attribute.

i dont know what your reason is to define it as an attribute, but what you can do is create a directive

like so

myApp.directive('showsum ', function() {
   return {
       restrict: 'A', // restrict to an attribute so we can use it as such
       link: function(scope, element, attrs) {
            scope.showsum = true; // set the show sum expression so we can access it in the scope 
       }
   }
})

example:

http://plnkr.co/edit/mE5LrSMWdIwPRazEdD3b?p=preview

it will create a showsum attribute for the scope and you can do w.e you want with it