I think the simplest / cleanest answer has not yet been included for this question. This answer also fits within the HTML5 Spec for a boolean attribute - http://www.w3.org/TR/html5/infrastructure.html#boolean-attributes
2.5.2 Boolean attributes
A number of attributes are boolean attributes. The presence of a
boolean attribute on an element
represents the true value, and the
absence of the attribute represents
the false value.
If the attribute is present, its value must either be the empty string
or a value that is an ASCII
case-insensitive match for the
attribute's canonical name, with no
leading or trailing whitespace.
The values "true" and "false" are not allowed on boolean attributes. To
represent a false value, the attribute
has to be omitted altogether.
HTML:
<city-zip city="clientCity" zip="clientZip" requiredParam></city-zip>
And the directive:
.directive('cityZip', function() {
return {
restrict: 'E',
templateUrl: '../templates/templateCityZip.html',
scope: {
city: '=',
zip: '='
},
controller: function($scope, $attrs) {
$scope.requiredParamExists = $attrs.hasOwnProperty( 'requiredParam' );
}
}
});
Simple, fits cleanly with HTML5 spec for boolean attributes, and no need to coerce a string to a scope variable ('requiredParam': '='
).
Note in the example code above, if minified, the required variables $scope
and $attrs
will change to a shorter string and break the code, but that's another issue.