2017-12-18 143 views
0

这是一个非常基本的ng-click示例。我似乎无法得到它的工作。不知道问题是什么。我发誓我之前使用过Angular!这里有一个jsbin:基本的ng-click示例不工作

JS Bin

<!DOCTYPE html> 
<html ng-app> 
<head> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width"> 
    <title>JS Bin</title> 
</head> 
<body> 
    <h1>{{1 + 2}}</h1> 
    <button ng-click="alert('hello world')">Click me!</button> 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.min.js"></script> 

</body> 
</html> 

模块:

angular.module('app', []); 
+0

添加控制器 –

回答

2

这将是对你有所帮助,而这样做angularjs你应该添加控制器,根据你的情况,我创建了一个功能,传递一个字符串值来显示警报的消息。

var app=angular.module('app', []); 
 
app.controller('testCtrl',function($scope){ 
 
    $scope.showalert=function(str){ 
 
     alert(str); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="app" ng-strict-di ng-controller="testCtrl"> 
 
<h1>{{1 + 2}}</h1> 
 
<button ng-click="showalert('hai this is angularjs click event')"> click Me!..</button> 
 
</div>

+0

谢谢!我认为使用警报应该可以使用或不使用控制器 – PPJN