Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

angularjs - Angular controller is not registered error

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

I have this apptest.js script:

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;
        });
    });
});

And here my data/data.json data:

[
  {
    "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":"[email protected]"
    },
    "categories":[
      "Vehicles",
      "Parts and Accessories"
    ],
    "image": "http://www.louisianasportsman.com/classifieds/pics/p1358549934434943.jpg",
    "views":213
  }
]

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 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

Any help is appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...