gpt4 book ai didi

javascript - Angular JS 范围变量不起作用

转载 作者:行者123 更新时间:2023-11-28 19:29:48 25 4
gpt4 key购买 nike

学习使用 angularJS,

我的index.html中有这个特定的代码。动态类型在上部起作用,但注入(inject)不起作用。可能出了什么问题?

代码:

<!DOCTYPE html>
<html>
<head>
<title>Test1</title>
</head>
<body ng-app="">
<!-- Dynamic Type -->

<input type="text" ng-model="name1" /><br>
<h1>Hello {{name1}}</h1>

<!-- End dynamic Type -->

<!-- Scope Injection begin-->
<div class="container" ng-controller="myController">
<input type="text" ng-model="naam" /><br>
<h3>Looping</h3>
<ul>
<li data-ng-repeat="cust in customerlist | filter:naam | orderBy:'city'"> {{cust.name | uppercase}} - {{cust.city}} </li>
</ul>
</div>
<script type="text/javascript" src="js/angular.min.js"></script>
<script type="text/javascript">
function myController ($scope) {

$scope.customerlist = [
{name: 'Raj Bannerjee', city: 'Zaire'},
{name: 'Prasun Bannerjee', city: 'Udaipur'},
{name: 'Raj Malhotra', city: 'Dubai'},
{name: 'Prasun Joshi', city: 'Mumbai'}
];

}

</script>
</body>
</html>

最佳答案

从版本 1.3 开始,使用全局 Controller 构造函数禁用了 Angular :

https://github.com/angular/angular.js/commit/3f2232b5a181512fac23775b1df4a6ebda67d018

With the exception of simple demos, it is not helpful to use globals for controller constructors. This adds a new method to $controllerProvider to re-enable the old behavior, but disables this feature by default.

BREAKING CHANGE: $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.

所以你必须像这样重构它:

angular.module('app',[])

.controller('myController', ['$scope', function($scope) {
$scope.customerlist = [
{name: 'Raj Bannerjee', city: 'Zaire'},
{name: 'Prasun Bannerjee', city: 'Udaipur'},
{name: 'Raj Malhotra', city: 'Dubai'},
{name: 'Prasun Joshi', city: 'Mumbai'}
];
}]);

另请参阅您的模块:

<body ng-app="app">

关于javascript - Angular JS 范围变量不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27087038/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com