警报窗口不使用角度

时间:2014-07-24 21:42:55

标签: html angularjs

我有以下代码,我想在单击按钮时显示警告:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body ng-app>
<button ng-click="alert('test')">Display Text</button>
<script src="angular.min.js"></script>
<script type="text/javascript">document.write("<base href=\"" + document.location + "\" />");</script>
<script type="text/javascript">
    $scope.alert = function(variable) {
        alert(variable);
    };
</script>
</body>
</html>

控制台将此显示为错误:Uncaught ReferenceError: $scope is not defined

1 个答案:

答案 0 :(得分:1)

<!DOCTYPE html>
<html lang="en" ng-app="App">
<head>
    <title></title>
</head>
<body ng-controller="ModelCtrl">
<button ng-click="alert('test')">Display Text</button>
<script src="angular.min.js"></script>
<script type="text/javascript">document.write("<base href=\"" + document.location + "\" />");</script>
<script type="text/javascript">
     var app = angular.module('App', []);
     app.controller('ModelCtrl', function ($scope, $http) {
        $scope.alert = function(variable) {
            alert(variable);
        };
     });
</script>
</body>
</html>