AngularJS - 无法定义控制器

时间:2016-05-06 16:58:21

标签: javascript angularjs

我是AngularJS的新手。有谁知道我收到此错误的原因 - Argument 'activationCtrl' is not a function, got undefined

HTML -

<div class="form-group row">
    <label for="member" class="col-sm-2 form-control-label">Member</label>
    <div class="col-sm-10">
      <input type="number" class="form-control" id="memberId" placeholder="Member" ng-click = "activation.zipcode()">
    </div>
</div>

控制器 -

angular.module('app', [])
  .controller('activationCtrl', activationCtrl);

  function activationCtrl() {   
    var self = this;

self.zipCode = function() {
        console.log("zipcode");
}
  }

州 -

.when('/activation', {
        templateUrl: 'views/activation.html',
        controller: 'activationCtrl',
        controllerAs: 'activation'
      })

3 个答案:

答案 0 :(得分:0)

您需要将角度控制器注册到HTML,如下所示:

使用ng-controller="activationCtrl"

尝试将其放在html页面的body标签中:

<body ng-controller="activationCtrl">

答案 1 :(得分:0)

我认为问题可能是控制器和状态定义的错误顺序。您需要在状态(路由)之前定义控制器,因此您可以在那里使用它。

Alternitavely您可以跳过模块中的控制器注册,只需将其定义为具有angular.module('app', ['ngRoute']); angular.module('app') .config([$routeProvider, function($routeProvider) { routeProvider .when('/activation', { templateUrl: 'views/activation.html', controller: activationCtrl, controllerAs: 'activation' }) }]); function activationCtrl() { var self = this; self.zipCode = function() { console.log("zipcode"); } } activationCtrl.$inject = []; 属性的单独函数,并在状态(路由)定义中传递此函数。 E.g:

     $toemail = "$appemail";
            $subject = "Bursary Application";

            $headers = "MIME-Version: 1.0\r\n"
                ."From: Tem <tem@here.ca>\r\n"
                ."Content-type: text/html; charset=ISO-8859-1\r\n";

            $body =     "<h3>Thank You ".$appfirstname." for applying for the Bursary.</h3>"
                ."<hr />"
                ."<h2>Bursary Application Form</h2>"
                ."<hr />"
                ."<h4>Application Date: ".$appdate."</h4>"
                ."<h4>First Name: ".$appfirstname."</h4>"
                ."<h4>Last Name: ".$applastname."</h4>"
                ."<h4>Birthdate: ".$birthdate."</h4>"
                ."<h4>Mailing Address: ".$appmailingaddress."</h4>"
                ."<h4>City: ".$appcity."</h4>"
                ."<h4>Province: ".$appprovstate."</h4>"
                ."<h4>Postal Code: ".$apppc."</h4>"
                ."<h4>Phone Number: ".$appphone."</h4>"
                ."<h4>Graduation Date: ".$graddate."</h4>"
                ."<h4>Email Address: ".$appemail."</h4>"
                ."<h4>Post Secondary Plans: ".$psplans."</h4>"
                ."<h4>Musical Accomplishments: ".$ma."</h4>"
                ."<h4>Hobbies: ".$hobbies."</h4>"
                ."<h4>Why I enjoy music: ".$why."</h4>"
                ."<h4>How I will use the monetary award: ".$how."</h4>"
                ."<h4>What I would say to the donor: ".$saydonor."</h4>"
                ."<br>"
                ."<br>"
                ."<hr />"
                ."<h4>Thank you for your application.</h4>"
                ."<br>"
                ."<br>"
                ;

            mail($toemail, $subject, $body, $headers);

答案 2 :(得分:0)

Argument&#39; activationCtrl&#39;不是一个功能,未定义

定义控制器时出现上述错误,但您不在html中导入activationCtrl.js文件。