Skip to content

Instantly share code, notes, and snippets.

@ahmednuaman
Created September 22, 2014 08:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmednuaman/ae21ced23d253ce47dc7 to your computer and use it in GitHub Desktop.
Save ahmednuaman/ae21ced23d253ce47dc7 to your computer and use it in GitHub Desktop.
The difference between @, =, and & in AngularJS directives
<div class="foo">
<ul>
<li data-ng-repeat="item in items">{{item}}</li>
</ul>
</div>
define([
'config',
'directive/snowball-directive'
], function (cfg, DRCT) {
DRCT('foo', [
'$rootScope'
], function ($rootScope) {
return {
templateUrl: cfg.path.partial + 'directive/foo-directive-partial.html',
restrict: 'A',
replace: true,
scope: {
items: '=',
vari: '@',
callback: '&'
},
link: function ($scope, $element, $attrs) {
$scope.$watch('vari', function () {
if ($scope.vari === 'bar') {
$scope.items.pop();
$scope.callback({
foo: $scope.vari
});
}
});
}
};
});
});
define([
'config',
'angular',
'text!partial/directive/foo-directive-partial.html',
'directive/foo-directive'
], function (cfg, A, partial) {
describe('Foo directive', function () {
var html = '<div data-foo data-items="dataItems" data-vari="{{foo}}" data-callback="callback(foo)"></div>',
$httpBackend,
$scope,
createDirective,
el;
beforeEach(module(cfg.ngApp));
beforeEach(inject(function ($injector) {
var $compile = $injector.get('$compile'),
$rootScope = $injector.get('$rootScope'),
compiled;
$httpBackend = $injector.get('$httpBackend');
$scope = $rootScope.$new();
el = A.element(html);
compiled = $compile(el);
createDirective = function () {
var directive = compiled($scope);
$scope.$apply();
return directive;
};
}));
afterEach(function () {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
it('should load', function () {
var data = [1, 2, 3, 4],
foo = 'foo',
callback = jasmine.createSpy('callback');
$httpBackend.expectGET(cfg.path.partial + 'directive/foo-directive-partial.html')
.respond(201, partial);
$scope.dataItems = data;
$scope.foo = foo;
$scope.callback = callback;
createDirective();
$httpBackend.flush();
expect(el.find('li').length).toBe(data.length);
$scope.foo = 'bar';
$scope.$apply();
expect($scope.dataItems.length).toBe(3);
expect($scope.callback).toHaveBeenCalledWith($scope.foo);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment