无法访问茉莉花中的控制器

时间:2018-02-26 10:12:22

标签: angularjs typescript text jasmine

angularjs&&打字稿控制器

   module app.controller {
   class MainCtrl {
   static $inject = ['$scope', '$rootScope', '$interval', '$http', 'unityService',
   'adalAuthenticationService', '$location', '$window', 'analyticsService', 'homePageSetting',
   'unityCommonService', '$timeout', '$cookies', '$routeParams', '$compile', '$filter'];
   }
   constructor(
   private $scope,
   private $rootScope,
   private $interval,
   private $http,
   private unityService: app.services.unityService,
   private adalAuthenticationService,
   private $location,
   private $window,
   private analyticsService,
   private homePageSetting,
   private unityCommonService,
   private $timeout,
   private $cookies,
   private $routeParams,
   private $compile,
   private $filter

       ) { }
   angular.module('UnityApp').controller('MainCtrl', MainCtrl);
   }

   Jasmine Test For Controller

   beforeEach(function () { angular.module("UnityApp") });

   it("should make a string more exciting", inject(function ($controller, $rootScope) {
   var controller =
   $controller('MainCtrl', {
   $scope: $rootScope.$new(), // scope 
   });
    //controller got undefined error when i access the controller ?? i tried many ways but i m unable to get the controller 
   }));

我无法访问MainCtrl中的范围

1 个答案:

答案 0 :(得分:1)

您要做的是创建范围并在初始化控制器之前将其分配给局部变量。

it("should make a string more exciting", inject(function ($controller, $rootScope) {

  var $myScope = $rootScope.$new();

  $controller('MainCtrl', {
    $scope: $myScope
  });

}));

然后,您就可以访问$ myScope中的控制器范围。