javascript - How to test $element in AngularJS controller using Karma? -


i having problem have controller in app use <div ng-controller='logbookeditctrl'> </div> , controller has $element provider in need modify element.

describe('logbookeditctrl', function(){   'use strict';    beforeeach(module('logbooks.edit'));     it('should create "logbook" model', inject(function($controller) {     var scope = {},     ctrl = $controller('logbookeditctrl', {$scope: scope}); //this explodes because says $element provider not found, because there no html element of course..the controller being created on own.     }));  }); 

i have tried following, again says $element provider not found :

beforeeach(inject(function(_$element_) {   var element = compile('<div></div>');   $element = element; })); 

you need pass dependency itself, since refers bound element of controller. , there no $element provider available in angular (much similar $scope these dynamic special dependencies provided app injector). create element object using angular.element , pass in locals controller constructor,something this.

var scope = {},      element = angular.element('<div></div>'); //provide element want test  ctrl = $controller('logbookeditctrl', {$scope: scope, $element:element }); 

this opens fact why not recommended use $element inside controller , perform dom operation in them.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -