【发布时间】:2016-05-04 14:52:00
【问题描述】:
当我在框中键入内容时,h1 文本会相应更改。所以,ng-model 工作正常。
但是,alert 函数永远不会被触发。为什么?
<html lang="en" ng-app>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="css/bootstrap.min.css">
<title></title>
<script type="text/javascript" src="angular.min.js"></script>
</head>
<body>
<input type="text" ng-model="name" ng-change="alert('name changed')">
<h1>Hello3, {{name}}</h1>
</body>
</html>
Angular 1.5.5
更新 这是带有@stackdisplay评论的工作版本:
<html lang="en" ng-app="myApp" ng-controller="AppCtrl">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="bootstrap.min.css">
<title></title>
<script type="text/javascript" src="angular.min.js"></script>
<script type="text/javascript">
var app = angular.module("myApp",[]);
app.controller('AppCtrl', function($scope) {
$scope.myfunc = function(text) { window.alert(text); }
})
</script>
</head>
<body>
<input type="text" ng-model="name" ng-change="myfunc('name changed')">
<h1>Hello, {{name}}</h1>
</body>
</html>
这是有效的。但是,如果我不实现myfunc(或以前的alert 函数),为什么我不会收到控制台错误?
如果window 是一个全局变量(同样,控制台中没有任何错误),为什么使用@think_win_win 建议的window.alert 不起作用?
【问题讨论】:
-
尝试写
window.alert('name changed')。该函数将根据当前的 $scope 进行评估,因为您可能没有定义 $scope.alert 函数,所以没有任何反应。 -
我试过
window.alert,但两者都没有。另外,我在 chrome 开发者控制台上没有收到任何错误消息。 -
该语法正在触发 $scope.alert ,因此如果您没有实际的 $scope.alert 函数,则不会发生任何事情。
标签: angularjs