2013-04-07 65 views
2

我玩弄https://github.com/angular/angular-seed像angular.module定义测量角控制器( '对myApp')。控制器(

控制器在应用程序定义/ controllers.js这样

'use strict'; 
function MyCtrl1() {} 
MyCtrl1.$inject = []; 

这并未“T通过jshint如MyCtrl1在app/app.js在我的全局列表中引用而不是。

根据Brian Ford和其他人我看过的首选风格是

angular.module('myApp').controller('MyCtrl1', [], function() {}); 

我喜欢这个更好,因为它不是在全球范围内,但现在我的testacular规格失败,因为这不工作了:

var myCtrl1; 
beforeEach(function(){ 
    myCtrl1 = new MyCtrl1(); 
}); 

我如何获得这个控制器的引用,其定义用于测试目的的“首选”样式?

+1

这可能是我已经anwsered [这里]同样的问题(http://stackoverflow.com/questions/15799853/angularjs-testing-controller/15801116#15801116)你需要添加'beforeEach(module('myApp'));' – Xesued 2013-04-08 00:05:30

回答

3

信贷由于双方Javito和Xesued:

beforeEach(module('myApp')); 
var scope, ctrl; 
beforeEach(inject(function($controller, $rootScope) { 
    scope = $rootScope.$new(); 
    ctrl = $controller('MyCtrl1', {$scope: scope}); 
})); 
0

尝试,

beforeEach(inject(function($controller) { 
    scope = {};  
    MyCtrl1 = $controller('MyCtrl1', { 
     $scope: scope 
    }); 
    })); 
+0

关闭,看来你需要一个真正的作用域,并且也可以像Xesued提出的beforeEach注释。代码没有在这里工作,我想我必须自己回答。对不起,我想给你信用:( – user2240431 2013-04-10 14:17:04