javascript - Error: "angular was used before it was defined" but online editors able to output the result -


i couldn't output code when ran local machine weird part online editors able give me output: http://codepen.io/anon/pen/waxgom

*im using adobe brackets editor

here html code:

    <!doctype html >   <html>     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>     <script src= "app.js"></script>         <body ng-app="myapp">  <div ng-controller="controller2">     <button ng-click="add()">add object</button>     <button ng-click="reset()">reset</button>     <div remove="remove($index)" ng-repeat="name in names">         {{name}}     </div> </div>  </body> </html> 

here js code:

var myapp = angular.module('myapp', []);      myapp.controller('controller2', function ($scope) {         $scope.names = [];     var data = ['dean', 'andy', 'dan', 'wizek', 'pete', 'pawel', 'chris', 'misko'];     $scope.add = function() {         if (data.length)             $scope.names.push(data.pop());     };          $scope.reset = function(index) {         data.push($scope.names.splice(index, 1)[0]);     };     }); 

please me figure out...

do mean see angular used before defined error jslint? that'd because you're using angular, global, in javascript code without letting jslint know it's in scope first.

jslint operates on page-by-page level, , can't tell what's in global context outside includes. you've loaded angular global context in include, jslint can't tell when looks @ single javascript file itself.

to clear, then, line should cause jslint report error:

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

you have add global directive let jslint know what's in global context.

/*global angular */ var myapp = angular.module('myapp', []); 

now you're golden.


just fun, here's jslinted version of javascript file snippet. didn't take much. note had add squiggly brackets around if block , jslint directive white:true considers non-standard whitespace. more on here.

/*jslint white:true */ /*global angular */ var myapp = angular.module('myapp', []); myapp.controller('controller2', function ($scope) {     "use strict";      $scope.names = [];     var data = ['dean', 'andy', 'dan', 'wizek', 'pete', 'pawel', 'chris', 'misko'];     $scope.add = function() {         if (data.length)         {             $scope.names.push(data.pop());         }     };      $scope.reset = function(index) {         data.push($scope.names.splice(index, 1)[0]);     }; }); 

Comments