我是AngularJS的新手。
我在跟踪MEAN :构建端到端的应用程序
我有一个简单的html文件来测试安古拉杰。
index.html
<html>
<head>
<meta charset="utf-8">
<title>Stack POC</title>
<script type="application/javascript" src="app/scripts/controllers/user_controller.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
</head>
<body ng-app="userListApp">
<div ng-controller="userListController">
<h1>No of users {{userCounts}}</h1>
</div>
</body>
</html>app/脚本/控制器/user_Controller.js
angular.module('userListApp', []).controller('userListController', function($scope){
$scope.userCounts = 10;
});但当我试着做这件事的时候,这是不给予的。
用户{{ userCounts }}的编号
而不是计数中的10。
我使用大口吞下和吞咽连接运行开发服务器。
gulpfile.js
var gulp = require('gulp'),
connect = require('gulp-connect');
gulp.task('serve', function() {
connect.server({
root: '.',
port: 8888
});
});
gulp.task('default', ['serve']);package.json
{
"name": "poc",
"version": "1.0.0",
"description": "",
"main": "gulpfile.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"gulp": "^3.9.1",
"gulp-connect": "^3.2.2"
}
}使用火狐 45.0.2查看此页面。
发布于 2016-04-20 17:43:15
这是因为你只是提供一个单一的函数,而不是提供一个完整的控制器。
试着做这样的事情:
<html>
<head>
<meta charset="utf-8">
<title>Stack POC</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
<script>
angular.module('userListApp', []).controller('userListController', function($scope){
$scope.userCounts = 10;
});
</script>
</head>
<body ng-app="userListApp">
<div ng-controller="userListController">
<h1>No of users {{ userCounts }}</h1>
</div>
</body>
</html>您需要一个由angular.module()函数创建的模块(我在示例中将它命名为angular.module)。第二个参数是依赖项(空数组表示无)。在这个模块上,您可以构建一个控制器。您必须将$scope作为参数传递给控制器函数,才能在示例中使用它。在控制器中,您可以像在前面的函数中尝试的那样,分配引用的变量。
发布于 2016-04-20 17:41:41
您不会在脚本中的任何地方实例化AngularJS应用程序。您需要在ng-app标记中将应用程序名称作为参数提供给body,并修改script。
userListController函数需要使用app.controller连接到AngularJS控制器。
请参考下列工作小提琴:
https://stackoverflow.com/questions/36751021
复制相似问题