MonsterJava
MonsterJava

Reputation: 423

VM73:49Uncaught ReferenceError: angular is not defined in jsfiddle

I am trying to run an angularJS example in jsfiddle (ofcourse learning purpose) getting below errors in console.

When I add this cdn for angular compatibility: https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.6/angular.min.js

I seen below error.

VM141 angular.min.js:6 Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.5.6/$injector/modulerr?p0=myApp&p1=Error%3A%2…oudflare.com%2Fajax%2Flibs%2Fangular.js%2F1.5.6%2Fangular.min.js%3A21%3A19)

When I remove the external source, seen below error:

VM93:45 Uncaught ReferenceError: angular is not defined

Concern:

  1. Do I need to add external source for angular example? If yes then what will be the correct way to add external resource. (like, search a cdn for that api and add).

Here's the code snippet:

html:

<div ng-app="myApp" ng-controller="myCtrl">
Your name: <input type="text" ng-model="name" ng-click="displayName();">
Hello {{$scope.name}}
</div>

js:

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

myApp.controller('myCtrl', function($scope){
$scope.displayName = function(){
alert("In right place");
$scope.name = "Name";
}
});

Here's the fiddle: https://jsfiddle.net/supdas87/my1juu4e/2/

Upvotes: 0

Views: 717

Answers (1)

Mistalis
Mistalis

Reputation: 18279

I've added angular-1.0.1.js file to external resources (you just have to copy and paste the URL), and changed {{$scope.name}} by {{name}} on your HTML file. You do not have to write $scope when you use {{ }}, it is implicit.

Here is a working Fiddle based on the code you provided.

PS: I've added Angular 1.0.1, but of course you can use the version of Angular you want. You can see allreleases here.


HTML

<div ng-app="myApp" ng-controller="myCtrl">
  Your name: <input type="text" ng-model="name" ng-click="displayName();">
  Hello {{name}}
</div>

JavaScript

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

myApp.controller('myCtrl', function($scope) {
  $scope.displayName = function(){
    alert("In right place");
    $scope.name = "Name";
  }
});

Upvotes: 2

Related Questions