执行angularJS窗口警报不起作用
Execise angularJS windows alert not woring
我有一个简单的问题,我复制并粘贴了一些代码,我想在其中显示一个带有值的警报。我只是在学习角度,功能正常(我在其中放置了一个控制台日志并执行它),但window.alert没有出现。
我已经阅读了本教程,并尝试更改变量的名称,但它仍然不起作用。在语言的在线教程中,一切都很顺利,那么我的错误在哪里呢?
这是页面代码:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" charset="utf-8" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.min.js"></script>
<script type="text/javascript" charset="utf-8" src="greetings.js"></script>
</head>
<body>
<h1>Greetings</h1>
<div ng-app="greetings" ng-controller="GreetingsController as greeting">
<b>Greetings:</b>
<div>
Name: <input type="text" min="0" ng-model="greeting.name" required >
</div>
<div>
FamilyName: <input type="text" ng-model="greeting.familyName" required >
<select
data-ng-options="c for c in greeting.tipi" data-ng-model="greeting.selectedOption">
<!--<option ng-repeat="c in greeting.tipi">{{c}}</option>-->
</select>
</div>
<div>
<b>Greeting:</b>
<span>
{{greeting.total(greeting.selectedOption)}} {{greeting.name}} {{greeting.familyName}}
<button class="btn" ng-click="invoice.greet(greeting.selectedOption)">Greet</button>
<br/>
</span>
</div>
</div>
</body>
</html>
这是控制器代码问候.js:
angular.module('greetings', [])
.controller('GreetingsController', function() {
this.name = "Name";
this.familyName = "FamilyName";
this.tipi = ["Hello", "Good Morning", "Good Afternoon", "Good evening"];
this.selectedOption = this.tipi[0];
this.total = function total(outGree) {
console.log(outGree);
return outGree;
};
this.greet = function greet(outGree) {
console.log(outGree);
$window.alert(outGree);
};
});
To use an Angular service, you add it as a dependency for the component (controller, service, filter or directive) that depends on the service.
And $window is a service.
您应该添加参数或依赖项$window来使用此服务。
angular.module('greetings', [])
.controller('GreetingsController', ['$window', function($window) {
this.name = "Name";
this.familyName = "FamilyName";
this.tipi = ["Hello", "Good Morning", "Good Afternoon", "Good evening"];
this.selectedOption = this.tipi[0];
this.total = function total(outGree) {
console.log(outGree);
return outGree;
};
this.greet = function greet(outGree) {
console.log(outGree);
$window.alert(outGree);
};
}]);
您需要在依赖项中包含$window,否则您的控制器将其读取为未定义。
尝试以下操作:
angular.module('greetings', [])
.controller('GreetingsController', function($window) {
//Rest of your code
this.greet = function greet(outGree) {
console.log(outGree);
$window.alert(outGree);
};
});
我将$window作为参数添加到控制器的功能中。
问题出现在html代码中:
<button class="btn" ng-click="invoice.greet(greeting.selectedOption)">Greet</button>
应该是:
<button class="btn" ng-click="greeting.greet(greeting.selectedOption)">Greet</button>
相关文章:
- Angular$scope在模式窗口内不起作用
- JS-窗口宽度函数不起作用
- 剑道UI下拉列表在剑道窗口内不起作用
- 执行angularJS窗口警报不起作用
- FB.login有效,但弹出窗口不起作用;t关闭
- jquery 1.3.2中的多选在弹出窗口中不起作用
- JS窗口.位置不起作用
- 具有子窗口的窗口打开器在同一窗口中导航不起作用
- 窗口滚动不起作用
- 模式弹出窗口内的按钮点击不起作用
- 引导程序弹出窗口(bootbox)中的Javascript事件不起作用
- ng在ui gmap窗口内单击不起作用
- 按钮点击事件在弹出窗口中不起作用
- PHP变量作为Javascript弹出窗口不起作用
- 窗体窗口警报,不'不起作用
- 如果在外部单击,则隐藏弹出窗口.为什么我的脚本没有'不起作用
- Javascript按钮单击在Chrome扩展弹出窗口中不起作用
- 获取 HTML 父窗口将不起作用.建议
- 触摸事件在模式弹出窗口上不起作用
- Javascript窗口/图像加载时间 - 这应该不起作用,但它确实有效