admin管理员组文章数量:1440487
So I already know when I see the answer I'm going to feel really dumb, but I'm working through the AngularJS tutorials at egghead.io. When I do this:
<!DOCTYPE html>
<html>
<head>
<title>AngularJS Tutorials</title>
<link rel="stylesheet" href="/foundation/stylesheets/foundation.min.css">
<script src="js/main.js"></script>
<script src="lib/angular/angular.js"></script>
</body>
</head>
<body>
<div ng-app="myApp">
<div ng-controller="FirstCtrl">
<input type="text" ng-model="data.message">
<h1>{{ data.message }}</h1>
</div>
<div ng-controller="SecondCtrl">
<input type="text" ng-model="data.message">
<h1>{{ data.message }}</h1>
</div>
</div>
</html>
with my JavaScript file as so (named main.js):
var myApp = angular.module('myApp', []);
myApp.factory('Data', function() {
return { message: "I'm data from a service"}
})
function FirstCtrl($scope, Data) {
$scope.data = Data;
}
function SecondCtrl($scope, Data) {
$scope.data = Data;
}
My {{ data.message }}
isn't recognized as an AngularJS binding (i.e. in the page it shows up as
{{ data.message }}
instead of "I'm data from a service"
). But if I take all the JS code and put it into <script></script>
tags directly in the HTML it works fine. Am I missing a reference or something? Haven't seemed to find this anywhere, likely because it's really basic.
So I already know when I see the answer I'm going to feel really dumb, but I'm working through the AngularJS tutorials at egghead.io. When I do this:
<!DOCTYPE html>
<html>
<head>
<title>AngularJS Tutorials</title>
<link rel="stylesheet" href="/foundation/stylesheets/foundation.min.css">
<script src="js/main.js"></script>
<script src="lib/angular/angular.js"></script>
</body>
</head>
<body>
<div ng-app="myApp">
<div ng-controller="FirstCtrl">
<input type="text" ng-model="data.message">
<h1>{{ data.message }}</h1>
</div>
<div ng-controller="SecondCtrl">
<input type="text" ng-model="data.message">
<h1>{{ data.message }}</h1>
</div>
</div>
</html>
with my JavaScript file as so (named main.js):
var myApp = angular.module('myApp', []);
myApp.factory('Data', function() {
return { message: "I'm data from a service"}
})
function FirstCtrl($scope, Data) {
$scope.data = Data;
}
function SecondCtrl($scope, Data) {
$scope.data = Data;
}
My {{ data.message }}
isn't recognized as an AngularJS binding (i.e. in the page it shows up as
{{ data.message }}
instead of "I'm data from a service"
). But if I take all the JS code and put it into <script></script>
tags directly in the HTML it works fine. Am I missing a reference or something? Haven't seemed to find this anywhere, likely because it's really basic.
- As I said...I feel D-U-M-B. – Jordan Plahn Commented Aug 5, 2013 at 22:59
3 Answers
Reset to default 2I'm going to JUMP THE GUN and remend you switch these
<script src="js/main.js"></script>
<script src="lib/angular/angular.js"></script>
to
<script src="lib/angular/angular.js"></script>
<script src="js/main.js"></script>
If this works then that is because before you were trying to use angular when it wasn't event loaded.
Tip
Now, when I first started with javascript these sort of things used to happen to me a lot. The best thing for you to do is learn how to use the
console
it would have told you in the console something along the lines ofcannot read property 'module' of undefined
that meaning thatangular
isundefined
and therefore not loaded.
Load Angular before your controllers, not after
switch between the lines:
<script src="js/main.js"></script>
<script src="lib/angular/angular.js"></script>
you need to run main.js after angular is loaded
本文标签: javascriptAngularJS not recognizing controllerStack Overflow
版权声明:本文标题:javascript - AngularJS not recognizing controller - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745647234a2668209.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论