角度控制器未注册错误 [英] Angular controller is not registered error

查看:54
本文介绍了角度控制器未注册错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Angular JS的新手,正在使用1.6版本.

I am new to Angular JS and I am using the 1.6 version of it.

我有这个apptest.js脚本:

var myApp = angular.module("myApp", []);

(function(){
    "use strict";
    myApp.controller("productController", function($scope, $http)

    {
        $http.get('data/data.json').then(function(prd)
        {
            $scope.prd = prd.data;
        });
    });
});

这是我的data/data.json数据:

[
  {
    "id":"1",
    "title":"20 Foot Equipment Trailer",
    "description":"2013 rainbow trailer 20 feet x 82 inch deck area, two 5,000 lb axels, electric brakes, two pull out ramps, break away box, spare tire.",
    "price":6000,
    "posted":"2015-10-24",
    "contact": {
      "name":"John Doe",
      "phone":"(555) 555-5555",
      "email":"johndoe@gmail.com"
    },
    "categories":[
      "Vehicles",
      "Parts and Accessories"
    ],
    "image": "http://www.louisianasportsman.com/classifieds/pics/p1358549934434943.jpg",
    "views":213
  }
]

现在这是我指定ng-app和ng-controller的html页面:

Now here is my html page where I specified the ng-app and ng-controller:

<body ng-app="myApp" ng-controller="productController">
<div class="row">
    <nav class="navbar navbar-default">
      <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">Brand</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
          <ul class="nav navbar-nav">
            <li class="active"><a href="#">Add <span class="sr-only">(current)</span></a></li>
          </ul>
          <form class="navbar-form navbar-left">
            <div class="form-group">
              <input type="text" class="form-control" placeholder="Search">
            </div>
            <button type="submit" class="btn btn-default">Submit</button>
          </form>
        </div><!-- /.navbar-collapse -->
      </div><!-- /.container-fluid -->
    </nav>

    <div class="col-sm-4" ng-repeat="product in prd">
        <div class="panel panel-primary">
            <div class="panel-heading">{{product.title}}</div>
            <div class="panel-body">
                <img ng-src="{{product.image}}">
                {{product.price | currency}}
                {{product.description}}
            </div>
            <div class="panel-footer">a</div>
        </div>
    </div>
</div>

<script src="angular/angular.js"></script>
<script src="scripts/appTest.js"></script>
</body>

我仍然遇到以下错误,这对我来说是新的:

I am still getting the following error which is new for me:

angular.js:14239错误:[$ controller:ctrlreg]带有 名称'productController'未注册. http://errors.angularjs.org/1.6.0-rc.0/ $ controller/ctrlreg?p0 = productController

angular.js:14239 Error: [$controller:ctrlreg] The controller with the name 'productController' is not registered. http://errors.angularjs.org/1.6.0-rc.0/$controller/ctrlreg?p0=productController

感谢您的帮助.

推荐答案

这是因为您的Immediately Invoked Function Expression.您必须像下面这样更改它:

Its because of your Immediately Invoked Function Expression. you have to change it like below :

var myApp = angular.module("myApp", []);

(function(app){
  "use strict";
  app.controller("productController", function($scope, $http){
    $http.get('data/data.json').then(function(prd){
      $scope.prd = prd.data;
    });
  });
})(myApp);

这篇关于角度控制器未注册错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆