javascript - AngularJS controller does not work, why? (simple controller example) -
i saw video of introduction angularjs , use following example
the html:
<!doctype html> <html ng-app> <head lang="en"> <meta charset="utf-8"> <title></title> </head> <body ng-controller="maincontrl"> <h2>{{message}}</h2> <script src="bower_components/angular/angular.min.js"></script> <script src="js/test.js"></script> </body> </html>
the js:
var maincontrl = function($scope){ $scope.message = "hello friend"; };
in example i've seen structure works, when test not work. console gives me error:
error: [ng:areq] http://errors.angularjs.org/1.3.15/ng/areq?p0=maincontrl&p1=not%20a%20function%2c%20got%20undefined
can used in way controller? or bad practice
example in plunker: code example
i think, it's bad practice
you better use:
var myapp = angular.module('myapp', []); myapp.controller('maincontrl', ['$scope', function ($scope) { // code here ]);
and use in html:
<html ng-app="myapp"> <body ng-controller="maincontrl"> // code here </body> </html>
Comments
Post a Comment