简体   繁体   English

Angular.js,等效的控制器编写失败

[英]Angular.js, equivalent controller writing that fails

I'm starting with Angular.js and have a question; 我从Angular.js开始,有一个问题。 What's wrong with the second way to express the controller? 第二种表达控制器的方式有什么问题? Take a look at the jsfiddle below 看看下面的jsfiddle

http://jsfiddle.net/yDhv8/ http://jsfiddle.net/yDhv8/

function HelloCtrl($scope, testFactory, testFactory2)
{
    $scope.fromFactory = testFactory.sayHello("World");
    $scope.fromFactory2 = testFactory2.sayHello("World");
}

myApp.controller('GoodbyeCtrl', ['$scope', 'testFactory', 'testFactory2', function($scope, testFactory, testFactory2) {
    $scope.fromFactory = testFactory.sayGoodbye("World");
    $scope.fromFactory2 = testFactory2.sayGoodbye("World");
}]);

Any references that may be useful to understand what's going on will be appreciated, 任何对了解正在发生的事情可能有用的参考文献将不胜感激,

Cheers, 干杯,

If you run this in a javascript debugger, you'll find that the variable 'myApp' is not defined. 如果在javascript调试器中运行此程序,则会发现未定义变量“ myApp”。 You can either use the 'app' reference that you assigned to the original module call, or use the following syntax: 您可以使用分配给原始模块调用的“ app”引用,也可以使用以下语法:

angular.module('myApp').controller(...)

Everything is fine. 一切顺利。 you just got confused. 你只是感到困惑。 use app.controller 使用app.controller

myApp is the module name, not a variable name. myApp是模块名称,而不是变量名称。

app.controller(......)

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

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