Basic examples of scope in a controller... Local variable, $scope variable, each with corresponding functions and unit tests.
Last active
December 17, 2015 09:29
-
-
Save DavidFrahm/5587298 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
angular.module('Sample.Controllers', []) | |
.controller('SampleSimpleController', function($rootScope, $scope) { | |
console.log('SampleSimpleController()'); | |
this.testThisVar = 'Test this var'; | |
$scope.testScopeVar = 'Test scope var'; | |
this.appendTestThisVar = function(addString) { | |
this.testThisVar = this.testThisVar + ' + ' + addString; | |
}; | |
$scope.appendTestScopeVar = function(addString) { | |
$scope.testScopeVar = $scope.testScopeVar + ' + ' + addString; | |
}; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
describe('Sample Controllers', function() { | |
beforeEach(function() { | |
module('Sample.Controllers'); | |
}); | |
describe('SampleSimpleController', function() { | |
var scope, controller; | |
beforeEach(inject(function($rootScope, $controller) { | |
scope = $rootScope.$new(); | |
controller = $controller('SampleSimpleController', { | |
$scope: scope | |
}); | |
})); | |
it('should have a SampleSimpleController controller', function() { | |
console.log('controller: ' + angular.toJson(controller)); | |
expect(controller).toBeDefined(); | |
}); | |
it('should have a this variable', function() { | |
console.log('testThisVar: ' + controller.testThisVar); | |
expect(controller.testThisVar).toBeDefined(); | |
}); | |
it('should append this variable', function() { | |
console.log('testThisVar (before): ' + controller.testThisVar); | |
controller.appendTestThisVar('added string'); | |
console.log('testThisVar (after): ' + controller.testThisVar); | |
expect(controller.testThisVar).toEqual('Test this var + added string'); | |
}); | |
it('should have a scope variable', function() { | |
console.log('testScopeVar: ' + scope.testScopeVar); | |
expect(scope.testScopeVar).toBeDefined(); | |
}); | |
it('should append scope variable', function() { | |
console.log('testScopeVar (before): ' + scope.testScopeVar); | |
scope.appendTestScopeVar('added string'); | |
console.log('testScopeVar (after): ' + scope.testScopeVar); | |
expect(scope.testScopeVar).toEqual('Test scope var + added string'); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment