0

I have the following code in which I want to display an alert when a button is clicked:

<!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> 

The console is displaying this as an error: Uncaught ReferenceError: $scope is not defined

2
  • are you importing all the scripts correctly? Commented Jul 24, 2014 at 21:45
  • 2
    I think that $scope is only defined inside controllers where the $scope is injected, it can't be used as just a global variable. Commented Jul 24, 2014 at 21:47

1 Answer 1

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> 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.