Sonia A.
Sonia A.

Reputation: 31

Angularjs no module error

Not sure what's wrong with my code, I got error of angular.min.js:17 Uncaught Error: No module: MyApp

http://jsfiddle.net/9g4nxdar/

(function() {
  angular.module("MyApp")
    .controller('MyCtrl', myController);

  function myController() {
    var vm = this;

    var item = [{
      "name": "James",
      "age": 20
    }, {
      "name": "John",
      "age": 20
    }]


  }
})();

I did declare this in the view <div ng-app="MyApp">

Upvotes: 0

Views: 640

Answers (2)

user6801750
user6801750

Reputation: 242

Sonia A - In your jsfiddle, just change the "JavaScript Frameworks & Extensions" to "AngularJS 1.4.8" and it starts working correctly.

Below is the fork for the same jsfiddle: http://jsfiddle.net/vcvv04hb/

(function() {
  angular.module("MyApp",[])
    .controller('MyCtrl', myController);

  function myController() {
    var vm = this;
    vm.items =  [{
      "name": "James",
      "age": 20
    }, {
      "name": "John",
      "age": 20
    }];
  }
})();
<div ng-app="MyApp">
  <div ng-controller="MyCtrl as vm">
    <ul>
      <li ng-click="vm.viewDetail = true" ng-repeat="item in vm.items">{{item.name}}
        <small ng-show="vm.viewDetail">{{vm.item.age}}</small>
      </li>
    </ul>
  </div>
</div>

Upvotes: 0

cnorthfield
cnorthfield

Reputation: 3501

The issue is your just getting a reference to the module MyApp. To create the module MyApp you need to pass an array as the second parameter like so:

app.js

(function(){
    // create the module MyApp
    angular.module('MyApp', []);

})();

controller.js

(function() {

  // get the reference to MyApp and create the controller
  angular.module("MyApp")
    .controller('MyCtrl', myController);

  function myController() {
    var vm = this;

    var item = [{
      "name": "James",
      "age": 20
    }, {
      "name": "John",
      "age": 20
    }]

  }

})();

From the documentation:

angular.module(name, [requires], [configFn]);

requires - If specified then new module is being created. If unspecified then the module is being retrieved for further configuration.

Upvotes: 2

Related Questions