不知道为什么这不起作用
<body ng-app="main" id="homepage">
<h1 ng-controller="homepageCtrl">hello {{test}}</h1>
</body>
JS
var app = angular.module("main", [])
.controller("homepageCtrl", function($scope) {
$scope.test = "World!";
});
最佳答案
你的代码没问题。这是 jsfiddle 造成的麻烦。
从 onLoad
切换负载类型至No Wrap - in <head>
刚刚在你的 fiddle 上测试过。就像魅力一样。
为什么它有效?
默认情况下,JsFiddle 将代码包装在就绪或加载事件中,并且在编译代码时,Angular 可能无法在全局范围内获得所需的内容。 No wrap in <head>
只需将脚本加载到头部,无需换行。
如果您在 Angular 中经常使用 jsfiddle,请阅读本文,它会为您节省大量时间 - Using jsFiddle with Angular
关于javascript - AngularJS 入门失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37785034/