Rafael
Rafael

Reputation: 7746

Simple Angular code not working

HTML

<section ng-app="app">
     <table ng-controller="VoicemailsCtrl">
        <caption>{{test}}</caption>
    </table>
</section>

JS

var app = angular.module('app', [])
              .controller('VoicemailsCtrl', ['$scope', VoicemailsCtrl]);

function VoicemailsCtrl($scope, $http)
{
    $scope.vms = [1,2,3];
    $scope.test = 'this is a test';
}

Can be seen at:

http://jsfiddle.net/tx9nbo8g/6/

Upvotes: 0

Views: 73

Answers (4)

Darshan Patel
Darshan Patel

Reputation: 2899

Please find below working code :

Html:

<body ng-app="app">
    <section>
        <table ng-controller="VoicemailsCtrl">
            <caption>{{test}}</caption>
        </table>
    </section>
</body>

Js:

var app = angular.module('app', []);
app.controller('VoicemailsCtrl', ['$scope','$http',VoicemailsCtrl]); //You forgot to add $http

function VoicemailsCtrl($scope, $http) {
    $scope.vms = [1,2,3];
    $scope.test = 'this is a test';
}

http://plnkr.co/edit/qCsG4WzFc0Irt2RobLoC?p=preview

Upvotes: 2

Abhishek Prakash
Abhishek Prakash

Reputation: 964

You missed adding ng-app="app" in the section.

Check the updated fiddle Fiddle

Apart from this you need to add

angular 1.2.1

and

No wrap in head

in framework and extension. .

Upvotes: 2

Vineet
Vineet

Reputation: 4645

Your HTML should have to be changed. You need to tell angular where app is starting. Default ng-app is also a valid app declaration.

 <section ng-app="MyApp">
         <table ng-controller="VoicemailsCtrl">
            <caption>{{test}}</caption>
        </table>
    </section>

In controller

 angular.module('MyApp', [])
 .controller('VoicemailsCtrl', ['$scope', function ($scope){
    $scope.vms = [1,2,3];
    $scope.test = 'this is a test';
 }]);

fiddle

You should not use global declaration of controller. Its been deprecated in angular 1.3+ versions. You should use controller directive to declare a controller.

Upvotes: 1

Pankaj Parkar
Pankaj Parkar

Reputation: 136154

Add ng-app="app" on section element & also you need to change the script loading option inside your fiddle from OnLoad to No Wrap in <head/>

Markup

<section ng-app="app">
    <table ng-controller="VoicemailsCtrl">
        <caption>{{test}}</caption>
    </table>
</section>

Working Fiddle

Upvotes: 1

Related Questions