gpt4 book ai didi

javascript - 为什么 ng-click 不起作用?

转载 作者:搜寻专家 更新时间:2023-11-01 04:54:08 25 4
gpt4 key购买 nike

到底为什么这个简单的 ng-click 示例不起作用?我在生产中使用 angular 几个月了,但这个笨蛋让我感到困惑。

http://plnkr.co/edit/9Rp5JD?p=preview

如果您注意到,ng-show 属性正在工作...为什么没有点击?它总是提醒默认值而不是 Angular 点击。

<div ng-show="showTest" ng-click="alert('angular click')" onclick="alert('default click')">Default or angular click?</div>
<div ng-show="dontShowMeIsNotDefinedSoIWontShow" >Not showing me, I know angular is working!</div>

没有孤立的作用域,根本没有指令……只有一个 div。您可以删除 onclick ....另一个警报永远不会触发。我敢肯定,无论问题是什么,我都太近视了,找不到它。

其余无聊的配置:

angular.module('plunker', []);

var app = angular.module('plunker', [
'plunker'
]);



app.config( function( ) {
'use strict';
});



app.controller('MainCtrl', function($scope) {
var that = this;

$scope.showTest = true;

});

整个 html:

<!DOCTYPE html>
<html ng-app="plunker">

<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<link data-require="bootstrap-css@*" data-semver="3.2.0" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" />
<script>
document.write('<base href="' + document.location + '" />');
</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.2.x" src="https://code.angularjs.org/1.2.6/angular.js" data-semver="1.2.25"></script>

<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl as vm">

<br/><br/>

<div ng-show="showTest" ng-click="alert('angular click')" onclick="alert('default click')">Default or angular click?</div>
<div ng-show="dontShowMeIsNotDefinedSoIWontShow" >Not showing me, I know angular is working!</div>


</html>

最佳答案

Angular 的表达式不使用 eval 函数,因此除非您在作用域链中某处定义了名为 alert 的函数,否则它将不会执行。

https://docs.angularjs.org/guide/expression#context

Angular does not use JavaScript's eval() to evaluate expressions. Instead Angular's $parse service processes these expressions.

Angular expressions do not have access to global variables like window, document or location. This restriction is intentional. It prevents accidental access to the global state – a common source of subtle bugs.

Instead use services like $window and $location in functions called from expressions. Such services provide mockable access to globals.

所以你必须在你的作用域上定义这样一个函数:

app.controller('MainCtrl', function($scope,$window) { 
var that = this;

$scope.showTest = true;
$scope.alert = function() {
$window.alert('angular click');
};
});

演示

angular.module('plunker', []);

var app = angular.module('plunker', [
'plunker'
]);

app.config( function( ) {
'use strict';
});

app.controller('MainCtrl', function($scope,$window) {
var that = this;

$scope.showTest = true;
$scope.alert = function(text){
$window.alert(text);
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="plunker" ng-controller="MainCtrl">
<div ng-show="showTest" ng-click="alert('angular click')" onclick="alert('default click')">Default or angular click?</div>
<div ng-show="dontShowMeIsNotDefinedSoIWontShow" >Not showing me, I know angular is working!</div>
</div>

关于javascript - 为什么 ng-click 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27536484/

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