AngularJS - Controller not registered

蹲街弑〆低调 提交于 2019-12-12 02:33:50

问题


I am starting out on AngularJS Unit Testing and I am getting an error like Controller with name "SiteCtrl" is not registered.

What I am doing wrong here?

describe('#hasFormError', function () {

    var $rootScope,form, templateHtml, ctrl,$scope,scope;

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

    beforeEach(inject(function ($rootScope, $controller, $compile, $templateCache) {
        $scope = $rootScope.$new();
        ctrl = $controller;
        ctrl('SiteCtrl', { $scope: scope });

        var a1 = angular.element('<div class="page_container" ng-controller="SiteCtrl"><form name="shan"><div class="zip"><input ng-if="max_length == 5" type="text" name="zipcode" value="" ng-model="Sitezip.zipcode" ng-pattern="/^[0-9]*$/" ng-maxlength="max_length" required><input ng-if="max_length == 7" type="text" name="zipcode" value="" ng-model="store_zip.zipcode" ng-pattern="/^[a-zA-Z0-9]*$/" ng-maxlength="max_length" required></form></div>');


        $compile(a1)($scope);
       form = $scope.shan;  
        $scope.$apply()


    }));


    it("TestCase :controller  defined \n", function () {
        expect(ctrl).toBeTruthy();
    }); 

    it("TestZipCode", function () {
        form.zipcode.$setViewValue("4535433123123");
        expect(form.zipcode.$valid).toBeTruthy();
     });


});

回答1:


You have to inject 'SiteCtrl' in any js file After that you can use it.

app.controller("SiteCtrl",SiteCtrl);
SiteCtrl.$inject = ['$scope','$state','$filter'];


来源:https://stackoverflow.com/questions/42918514/angularjs-controller-not-registered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!