12

I want define a controller when use module:

angular.module('todoList', [], function () {

}).controller('myCtrl', function ($scope) {
    return function ($scope) {
        $scope.todos = [
            {
                text: "Hello"
            }, {
                text: "World"
            }
        ]
    }

})

then I want use the module and the ccontroller:

<div ng-controller="myCtrl" ng-app="todoList">  
        <li ng-repeat="todo in todos">
            <span>{{todo.text}}</span>
        </li>>
</div>

but it render nothing, what's wrong with my code?

5 Answers 5

15

Your controller is wrong, there is no need to have a return function.

angular.module('todoList', [], function () {

}).controller('myCtrl', function ($scope) {

        $scope.todos = [
            {
                text: "Hello"
            }, {
                text: "World"
            }
        ]
})

Demo: Plunker

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

5 Comments

How would I create an instance method on the myCtrl controller?
what do you mean by an instance method
I think my problem is more with ng-click no linking up somewhere. When I click a button I want to call a function on the controller rather than reading a property
@SomethingOn It will be better if you can ask a new question with your code shared
2

Official Docs:

http://docs.angularjs.org/guide/controller

Syntax:

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

myApp.controller('GreetingController', ['$scope', function($scope) {
  $scope.greeting = 'Hola!';
}]);

Comments

1
var myName1=angular.module("myName",[]);
myName1.controller("nameInfo",["$scope",function($scope){
$scope.name="Rajnish";
$scope.address="Delhi";
}
])

Comments

0

Not much different but this one works too.

angular.module('todoList', []).
    controller('myCtrl', 
      function ($scope){
       $scope.todos=[{text: "Hello"},{text: "World"}];
     });

Comments

0

You can do like this too by using JS strict mode-

(function() {
'use strict';
angular.module('listProject', []);
})();

(function() {
'use strict';
angular.module('listProject').controller('MyController', ['$scope', 
  function($scope) {
    console.log("Hello From ListProject Module->My Controller"):

   }

])

})();

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.