简体   繁体   English

尝试包含ng-controller时出错

[英]Error when trying to include ng-controller

I am following an angular.js tutorial, and I am being thrown errors when I am trying to include 'ng-controller' in the html tag like so. 我正在关注angular.js教程,并且试图在html标记中包含“ ng-controller”时遇到此类错误。

<html lang="en" ng-app ng-controller="AppCtrl">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Angular Js test</title>
    <link rel="stylesheet" href="static/css/bootstrap.min.css">
    <script src="static/js/jquery-1.11.3.min.js" type="text/javascript" charset="utf-8" async defer></script>
    <script src="static/js/bootstrap.min.js" type="text/javascript" charset="utf-8" async defer></script>
    <script src="static/js/angular.min.js" type="text/javascript" charset="utf-8" async defer></script>
    <script src="app/controller.js" type="text/javascript" charset="utf-8" async defer></script>
</head>
<body>

<input type="text" ng-model="name">

<h1>{{name || 'World'}}</h1>

</body>
</html>

The tutorial says to append it as a second attribute to the html tag but I am get the following errors when using it in the chrome browser 本教程说将其作为第二个属性附加到html标签,但是在chrome浏览器中使用它时出现以下错误

Error: [ng:areq] http://errors.angularjs.org/1.5.0-beta.2/ng/areq?p0=AppCtrl&p1=not%20a%20function%2C%20got%20undefined
    at Error (native)
    at http://test.dev/static/js/angular.min.js:6:423
    at rb (http://test.dev/static/js/angular.min.js:22:108)
    at Ra (http://test.dev/static/js/angular.min.js:22:195)
    at http://test.dev/static/js/angular.min.js:83:268
    at B (http://test.dev/static/js/angular.min.js:61:474)
    at w (http://test.dev/static/js/angular.min.js:62:403)
    at g (http://test.dev/static/js/angular.min.js:56:183)
    at http://test.dev/static/js/angular.min.js:55:329
    at http://test.dev/static/js/angular.min.js:20:103(anonymous function) @ angular.min.js:111(anonymous function) @ angular.min.js:84m.$apply @ angular.min.js:137(anonymous function) @ angular.min.js:20e @ angular.min.js:40c @ angular.min.js:19yc @ angular.min.js:20ae @ angular.min.js:19(anonymous function) @ angular.min.js:298j @ jquery-1.11.3.min.js:2k.fireWith @ jquery-1.11.3.min.js:2m.extend.ready @ jquery-1.11.3.min.js:2J @ jquery-1.11.3.min.js:2

My Controller is as follows 我的控制器如下

function AppCtrl ($scope) {
    $scope.name = "World";
}

create an app.js file 创建一个app.js文件

var app = angular.module('AppName', []);

app.controller('AppCtrl', function($scope) {
    $scope.name = "World";
});

load the app.js file in your index.html file 将app.js文件加载到index.html文件中

<script src="app.js" type="text/javascript" charset="utf-8" async defer></script>

in your index.html file add the following to your html tag 在index.html文件中,将以下内容添加到html标记中

<html lang="en" ng-app="AppName" ng-controller="AppCtrl">

<h1>{{ name }}</h1>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM