Skip to content

Instantly share code, notes, and snippets.

@levonlee
Last active August 29, 2015 14:23
Show Gist options
  • Save levonlee/c2c18577453b26462e1f to your computer and use it in GitHub Desktop.
Save levonlee/c2c18577453b26462e1f to your computer and use it in GitHub Desktop.

Use ng-attr-your-attribute to conditionally add or remove an attribute in HTML element. AngularJS 1.3 above

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.js"></script>
<div ng-app="myapp" ng-controller="parentCtrl">
<div ng-repeat="x in items" ng-attr-data-id="{{x.id || undefined}}">
<!-- Another way -->
<div ng-attr-data-id="{{ (show) ? x.id : undefined }}">
{{ repeat_title + ($index+1) + ': ' + x.name }}
</div>
</div>
</div>
var app=angular.module('myapp',[]);
app.controller('parentCtrl',function($scope) {
$scope.items = [{name: 'name1', id:1}
,{name: 'name2', id:2}];
$scope.repeat_title = 'Person #';
$scope.show = 1;
});
normalize_css: no
panel_jss: 0
panel_css: 0
wrap: b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment