Javascript ';控制器';单独定义返回不是一个函数,未定义

Javascript ';控制器';单独定义返回不是一个函数,未定义,javascript,angularjs,Javascript,Angularjs,不知道为什么这一次不管用以前每一个地方都管用这次它制造了这样的问题 这里我喜欢app.js var testApp= angular.module('test',[]); testApp.controller('testCtrl',function($scope){ $scope.testValue='testttttttttt'; }) 查看文件index.html <div ng-app="test" ng-controller="testCtrl"> {{testValue

不知道为什么这一次不管用以前每一个地方都管用这次它制造了这样的问题

这里我喜欢app.js

var testApp= angular.module('test',[]);

testApp.controller('testCtrl',function($scope){
$scope.testValue='testttttttttt';
})
查看文件index.html

<div ng-app="test" ng-controller="testCtrl">
{{testValue}}
</div>

它又起作用了


这里,主要的问题是什么??像这样我不能通过任何考试。。有什么建议吗

加载app.js文件后放置控制器文件

所以app.js的内容是

var testApp= angular.module('test',[]);
angular.module('test').controller('testCtrl',function($scope){
$scope.testValue='testttttttttt';
})
Controller.js将

var testApp= angular.module('test',[]);
angular.module('test').controller('testCtrl',function($scope){
$scope.testValue='testttttttttt';
})
在index.html中,顺序为

<script src="app.js"></script>
<script src="testCtrl.js"></script>


您正在为模块声明并重新声明一个全局变量。这个全局变量可能会被提升,或者其他某种javascript执行巫术会使代码以不可预测的方式运行。为了避免这种情况,只需使用angular的内置模块
getter
调用,而不是在全局名称空间中声明它

像这样:

App.js

angular.module('testApp', [
    // dependencies here'
]);
TestCtrl.js

angular.module('testApp')
    .controller(function($scope) {
        $scope.testValue = "value";
    });
同样,这里重要的区别是,
angular.module('testApp',[])
与第二个参数(依赖项列表)创建一个新模块,覆盖以前的
testApp
。另一方面,
angular.module('testApp')
在没有第二个参数的情况下调用,只需检索模块,以便您可以向其添加更多指令/控制器/配置/常量

此外,您可能应该抛弃独立控制器,因为它们不再被认为是最佳实践。指令/组件路线现在更加流行


有关当前angular最佳实践的简要概述,请查看John Papa的

您可以在单独的文件中共享控制器的代码吗?另外,需要注意的是,
var testApp=angular.module('test')
var testApp=angular.module('test',[])
不同。一个是声明一个新模块,另一个是检索一个先前存在的模块。如果在两个文件中都使用第二种格式,则只需重新创建模块。清除之前添加的所有内容。@suzo我一直在测试同一个测试控制器。。可能是因为
testApp
作为一个全局变量通常被认为是不好的形式。最好使用angular的预期getter方法
angular.module('testApp')
来检索要添加到的模块。如果我没有将testctrl.js放在app.js之后,那么这两种情况下都不应该起作用@sajee@AaronPool它甚至还没有进入控制器内部。不知道我为什么会出现这样的错误:(@BijayRai)你有没有检查我自己的答案。
angular.module('testApp')
    .controller(function($scope) {
        $scope.testValue = "value";
    });