为什么NG控制器不使用此功能这个例子中工作? [英] Why does ng-controller not work with function this this example?
问题描述
我试着沿着一个教程遵循但这code不为我工作。可有人expain为什么和如何解决呢?我认为它做NG-控制器,但不知道为什么。
<!DOCTYPE HTML>\r
\r
< HTML NG-应用>\r
< HEAD>\r
<标题> AngularJS 2'; /标题>\r
&所述; SCRIPT SRC =angular.min.js>&下; /脚本>\r
< /头>\r
\r
<机身NG控制器=myController的>\r
< H1> {{author.name}}< / H1>\r
&所述p为H.; {{author.title +','+ author.company}}&下; / P>\r
\r
<脚本>\r
功能myController的($范围){\r
$ scope.author = {\r
'名':'雷别墅',\r
标题:作者的工作人员,\r
'公司':'boss`enter code here`.com\r
}\r
}\r
< / SCRIPT>\r
\r
< /身体GT;\r
< / HTML>
\r
您code不会与角1.3+,因为你所定义的控制器作为全局函数工作。
的从AngularJS文档:的
迁移从1.2到1.3
控制器的
由于3f2232b5,$控制器将不再寻求在窗口控制器。寻找在窗口控制器旧行为最初是打算用于例子,演示和玩具应用的使用。我们发现,让全球控制器功能鼓励穷人的做法,所以我们决定禁用默认情况下此行为。
块引用>要迁移,注册模块的控制器,而不是将它们暴露为全局
定义控制器代替如下:
\r\r\r\r< HTML NG-应用=对myApp>\r
< HEAD>\r
<标题> AngularJS 2'; /标题>\r
&所述; SCRIPT SRC =https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js>&下; /脚本>\r
< /头>\r
\r
<机身NG控制器=myController的>\r
< H1> {{author.name}}< / H1>\r
&所述p为H.; {{author.title +','+ author.company}}&下; / P>\r
\r
<脚本>\r
angular.module('对myApp',[]);\r
\r
angular.module('对myApp')。控制器('myController的',函数($范围){\r
$ scope.author = {\r
'名':'雷别墅',\r
标题:作者的工作人员,\r
'公司':'boss`enter code here`.com\r
}\r
});\r
< / SCRIPT>\r
\r
< /身体GT;\r
\r
< / HTML>\r
Im trying to follow along a tutorial but this code doesn't work for me. Can someone expain why and how to solve it? I think its to do with ng-controller but not sure why.
<!doctype html> <html ng-app> <head> <title>AngularJS 2</title> <script src="angular.min.js"></script> </head> <body ng-controller="MyController"> <h1>{{author.name}}</h1> <p>{{ author.title + ', ' + author.company }}</p> <script> function MyController($scope) { $scope.author = { 'name' : 'Ray Villa', 'title' : 'Staff Author', 'company' : 'boss`enter code here`.com' } } </script> </body> </html>
解决方案Your code would not work with angular 1.3+ because your are defining the controller as a global function.
From AngularJS documentation :
Migrating from 1.2 to 1.3
Controllers
Due to 3f2232b5, $controller will no longer look for controllers on window. The old behavior of looking on window for controllers was originally intended for use in examples, demos, and toy apps. We found that allowing global controller functions encouraged poor practices, so we resolved to disable this behavior by default.
To migrate, register your controllers with modules rather than exposing them as globals Define the controller as follows instead :
<html ng-app="myApp"> <head> <title>AngularJS 2</title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script> </head> <body ng-controller="MyController"> <h1>{{author.name}}</h1> <p>{{ author.title + ', ' + author.company }}</p> <script> angular.module('myApp', []); angular.module('myApp').controller('MyController', function ($scope) { $scope.author = { 'name': 'Ray Villa', 'title': 'Staff Author', 'company': 'boss`enter code here`.com' } }); </script> </body> </html>
这篇关于为什么NG控制器不使用此功能这个例子中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!