1

Angular controller is not talking to view. ng-app is included, and also is ng-controller.

Using meanJS.

Here is the view:

<section ng-app="my-module" ng-controller="MyModuleController">
  <div>
    {{ costumers[0] }}
  </div>
</section>

Here is the controller:

(function() {
  'use strict';

  angular
    .module('my-module')
    .controller('MyModuleController', MyModuleController);

  MyModuleController.$inject = ['$scope'];

  function MyModuleController($scope) {
    var vm = this;

    // My module controller logic
    // ...

    $scope.costumers = [{
      name: 'John Doe',
      city: 'New York'
    }, {
      name: 'Peter Griffin',
      city: 'San Francisco'
    }];

    init();

    function init() {

    }
  }
})();
6
  • anything in Browser console? Commented Mar 27, 2017 at 4:33
  • Can you see the right HTML? Commented Mar 27, 2017 at 4:34
  • @Mr.Arjun angular.js:11706 Error: [ng:areq] Argument 'MymoduleController' is not a function, got undefined Commented Mar 27, 2017 at 4:36
  • If it is not existing App then module('my-module',[]) Commented Mar 27, 2017 at 4:36
  • @Michelem what do you mean? Commented Mar 27, 2017 at 4:37

2 Answers 2

1

Try to use vm instead the $scope:

function MyModuleController($scope) {
    var vm = this;

    vm.costumers = [{
      name: 'John Doe',
      city: 'New York'
    }, {
      name: 'Peter Griffin',
      city: 'San Francisco'
    }];

    init();

    function init() {

    }
  }

HTML:

<section ng-app="my-module" ng-controller="MyModuleController as vm">
  <div>
    {{ vm.costumers[0] }}
  </div>
</section>
Sign up to request clarification or add additional context in comments.

2 Comments

So you have other problems
Sorry I forgot you need to change also this: <section ng-app="my-module" ng-controller="MyModuleController as vm">
0

After loosing a few hair, i've solved the problem. The problem was in routing file, a simple misspelling in controller's name. "Argument 'MymoduleController' is not a function, got undefined" listed at the browser console, shows that it is spelled wrong. It was intended to be MyModuleController, not MymoduleController. Thanks for the help guys, problem solved!

Here is the fixed routing file:

(function() {
  'use strict';

  //Setting up route
  angular
    .module('my-module')
    .config(routeConfig);

  routeConfig.$inject = ['$stateProvider'];

  function routeConfig($stateProvider) {
    // My module state routing
    $stateProvider
      .state('my-module', {
        url: '/my-module',
        templateUrl: 'modules/my-module/client/views/my-module.client.view.html',
        controller: 'MyModuleController',
        controllerAs: 'vm'
      });
  }
})();

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.