javascript - What is wrong with this particular AngularJS code? -
so learning angularjs via several online websites after having familiarized myself with, , learned, basic js, html, , css. found 3 simple learn having bit of issue angularjs. @ beginning have question why code inputting not returning desired result. have 2 windows (or files... not sure correct vocab here) open. js 1 titled main.js , index file titled index.html. in main.js have:
var app= angular.module('myapp'),[]) app.controller('mycontroller', function($scope) { $scope.person = { name: "papa bear" }; });
in index file have:
<!doctype html> <html ng-app='myapp'> <head> <script src='js/main.js'></script> </head> <body> <h2> hi there </h2> <div ng-controller='mycontroller'> {{ person.name }} </div> </body> <html>
the "hi there" displays fine. {{ person.name }} (i certain) should return: papa bear shows literally "{{ person.name }}". error in code not connecting person.name papa bear?
yeah, kaveman pointed out, have have angular script included work. try adding head:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
and see if works.
edit also, noticed (thanks bhojendra nepal), this:
var app= angular.module('myapp'),[])
should be:
var app = angular.module('myapp',[]);
angular.module takes app name first argument, , array of dependencies second argument in order create new angular module. without second argument of array of dependencies, returns existing angular module of name.
so optionally, instead of assigning module variable, do:
angular.module('myapp',[]); angular.module('myapp').controller('mycontroller',function($scope){ $scope.person = { name: "papa bear" }; });
Comments
Post a Comment