Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular.js: How to create controller in module?

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?

like image 946
hh54188 Avatar asked Apr 01 '13 03:04

hh54188


3 Answers

Official Docs:

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

Syntax:

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

myApp.controller('GreetingController', ['$scope', function($scope) {
  $scope.greeting = 'Hola!';
}]);
like image 116
Bijan Avatar answered Oct 08 '22 15:10

Bijan


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

like image 15
Arun P Johny Avatar answered Oct 08 '22 17:10

Arun P Johny


var myName1=angular.module("myName",[]);
myName1.controller("nameInfo",["$scope",function($scope){
$scope.name="Rajnish";
$scope.address="Delhi";
}
])
like image 1
Rajnish Bakaria Avatar answered Oct 08 '22 16:10

Rajnish Bakaria