AngularJS - Simple Directive Use
Assign to scope
HTML
<link rel="stylesheet" href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css">
<script src="http://code.angularjs.org/1.0.0/angular-1.0.0.js"></script>
<div ng-app="myApp">
<div ui-foo>I should change iamfoo</div>
<br>
<div ui-bar att-label="HELLO">I should change to iambar</div>
</div>
CSS
.ui-state-highlight { background-color: #EEEEEE; }
JavaScript
angular.module('ui.directives', []).directive('uiBar',
function($parse,Data) {
return {
restrict: 'A',
link: function($scope, element, attrs, controller) {
var controllerOptions, options;
element.text("iambar " + attrs.attLabel);
$scope.label = attrs.attLabel;
}
};
}
)
.directive('uiFoo',
function(Data) {
return {
restrict: 'A',
link: function($scope, element, attrs, controller) {
var controllerOptions, options;
$scope.$watch(function(){return $scope.label;},function(newValue,oldValue) {
element.text("iamfoo " + $scope.label)
})
}
};
}
);
angular.module('myApp', ['ui.directives','ui.factory']);