简体   繁体   English

AngularJS ng-app声明的问题

[英]Issues with AngularJS ng-app declaration

I am starting with Angular and I am stuck with a strange issue that's baffling me. 我从Angular开始,我遇到了一个令我困惑的奇怪问题。

I have simple html and js as given below. 我有简单的html和js,如下所示。 If I add the angular.js file link as the last line inside BODY (as recommended in the docs) the binding fails due to some reason. 如果我将angular.js文件链接添加为BODY中的最后一行(如文档中所建议的那样),则由于某种原因绑定失败。 Similarly if I link the custom javascript file in the HEAD, the binding fails. 同样,如果我在HEAD中链接自定义javascript文件,则绑定失败。 I am sure I am missing something very basic here. 我相信我在这里遗漏了一些非常基本的东西。 Can't figure out what though.: 虽然无法弄明白:

HTML FILE: HTML文件:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"
>
<html lang="en">
<head>
<title>Angular Binding</title>
<link rel="stylesheet" href="../Foundation/css/foundation.min.css">
<script type="text/javascript" src="../JS/angular.js"></script>
</head>
<body>
<div ng-app="myapp"> 
    <input type="text" ng-model="data.message">
        {{data.message}}
</div>
<script type="text/javascript" src="BasicFilters.js"></script>
</body>
</html>

BASICFILTERS.JS FILE BASICFILTERS.JS文件

var myapp = angular.module('myapp', [])

Try moving your declaration to html tag: 尝试将您的声明移动到html标记:

<html lang="en" ng-app="myapp" xmlns:ng="http://angularjs.org">
<head>[...]

I had this same issue, was so brutal. 我有同样的问题,是如此残酷。 Then I asked a developer from work to help and when I showed him the issue, it started working... 然后我请求一位开发人员帮忙,当我向他展示这个问题时,它开始工作......

Anyway, one thing he showed me was that the controllers should be inside of a .controller that's tied to myApp, instead of in the global scope. 无论如何,他告诉我的一件事是控制器应该在与myApp绑定的.controller中,而不是在全局范围内。

So instead of: 所以代替:

function MyController1($scope) {
    $scope.data = { message: "Hello World" };
}

Place it inside of a .controller: 把它放在.controller里面:

myApp.controller('MyController1', ['$scope', function($scope) {
        $scope.data = { message: "Hello World" };
    }
]);

You can read more about this here . 你可以在这里阅读更多相关信息。

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

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