gpt4 book ai didi

javascript - Angular.JS - 添加 Controller 时出错

转载 作者:行者123 更新时间:2023-11-30 16:31:44 25 4
gpt4 key购买 nike

我是新来的 .我想要做的是在 Angular 应用程序中添加一个简单的 Controller 。所以,我的代码是这样的-

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div ng-app="">
<div ng-init="mySwitch=true">
<p>
<button ng-disabled="mySwitch">Click Me!</button>
</p>
<p>
<input type="checkbox" ng-model="mySwitch"/>Button Disable
</p>
<p>
{{ mySwitch }}
</p>
</div>
</div>
</body>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</html>

所以,它工作得很好-

enter image description here

当我这样添加 ng-controller="anything" 时...

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div ng-app="">
<div ng-controller="anything" ng-init="mySwitch=true">
<p>
<button ng-disabled="mySwitch">Click Me!</button>
</p>
<p>
<input type="checkbox" ng-model="mySwitch"/>Button Disable
</p>
<p>
{{ mySwitch }}
</p>
</div>
</div>
</body>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</html>

似乎没有任何效果。

enter image description here

谁能帮忙,我做错了什么?

在此先感谢您的帮助。

最佳答案

看起来您还没有定义 Controller 。观察以下...

<div ng-app="app">
<div ng-controller="ctrl">
[...]

angular.module('app', []).controller('ctrl', function($scope) {
$scope.mySwitch = true;
});

JSFiddle Link - 工作演示

和往常一样,引用 Understanding Controllers文档以获取更多信息。

关于javascript - Angular.JS - 添加 Controller 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33225687/

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