0

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

3
  • Add the dependencies array as a second parameter to your module declaration. angular.module('MyApp', []) Commented Sep 6, 2016 at 17:39
  • @PrateekGupta I see. Thanks, but what's wrong here? jsfiddle.net/qnr187m2 Commented Sep 6, 2016 at 17:56
  • 1
    @SoniaA. As i mentioned in my comment below my answer. AngularJS introduced support for “Controller as” syntax in version 1.1.5. You are using version 1.1.1 in that fiddle Commented Sep 6, 2016 at 18:08

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

any idea controller as doesn't work here? jsfiddle.net/qnr187m2
Yes, AngularJS introduced support for “Controller as” syntax in version 1.1.5. You are using version 1.1.1 in that fiddle :)
0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.