2015-12-30 63 views
1

我正在学习AngularJS,我正在玩弄依赖注入和内插。

我得到了开发工具以下两个错误中铬:

Uncaught Error: [$injector:modulerr] Failed to instantiate module myApp due to: 
Error: [$injector:nomod] Module 'myApp' is not available! You either misspelled the module name or forgot to load it. 

Uncaught SyntaxError: Unexpected token (on line9 app.js 

所以提到它明确指出,它无法找到该模块的第一个错误,我知道我已经corectly写它,并宣布index.html中的模块与data-ng-app="myApp"(我喜欢确保我符合html标准,因此我使用数据前缀),并且在html标记中引用时显然不会被拼写错误。

这里是app.js:

和index.html:

<!DOCTYPE html> 
<html lang="en" data-ng-app="myApp"> 
<head> 
    <script src="//code.angularjs.org/1.5.0-rc.0/angular.js"></script> 
    <script src="app.js"></script> 
    <meta charset="UTF-8"> 
    <title>Title</title> 
</head> 
<body> 
<div data-ng-controller="mainController"> 
    <label>What is your twitter handle? </label> 
    <input type="text" data-ng-model="handle"/> 
    <br/> 
    <h1>twitter.com/{{ lowercasehandle() }}</h1> 
</div> 
</body> 
</html> 

你可以从html标记我曾引用看到 '对myApp' 在顶部html标签模块。

至于与“意外的标记(”这是指这条线return $filter.('lowercase')($scope.handle);我没有看到任何东西,是无效的第二个错误。

我知道我失去了一些东西,但我不能看到

回答

1

你已经注意到这个错误Uncaught SyntaxError: Unexpected token (on line9 app.js

这个错误涉及到filter.('lowercase')filter('lowercase')

1

转换此

$filter.('lowercase')($scope.handle); 

这个

$filter('lowercase')($scope.handle); 
1

请检查这一个。

$scope.lowercasehandle = function(){ 
     return $filter('lowercase')($scope.handle); 
    }; 
1

作为每角文档here

我们定义了一个模块,如下所示:

var myApp = angular.module("myApp", []);

并限定出控制器,用于在模块myApp,则应该使用它作为

var app = angular.module("myApp"); 
app.controller("mainController", ['$scope', '$filter', function($scope, $filter){ 
$scope.handle = ''; 
$scope.lowercasehandle = function(){ 
    return $filter('lowercase')($scope.handle); 
}; 
}]);