user5622133 user5622133 - 10 days ago 5
AngularJS Question

script not working properly

<!DOCTYPE html>
<html >
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<body ng-app="notePad" ng-controller="notePadCtrl">

<textarea ng-model="message"></textarea>
<button ng-click="save()">save</button>
<button ng-click="clear()">clear</button>

<script src="notePad.js"></script>
<script src="notePadCtrl.js"></script>

</body>
</html>




var app = angular.module("notePad", [] );

app.controller("notePadCtrl",function($scope){
$scope.message="";
$scope.left=function(){
return 100 - $scope.message.lenght;
};
$scope.clear=function(){
$scope.message="";
};
$scope.save=function(){
alert("file got saved");
};
)};

Answer

there are several issues in this code. first the closing of the controller brackets should be changed

app.controller("notePadCtrl",function($scope){
    $scope.message="";
    $scope.left=function(){
        return 100 - $scope.message.lenght;
    };
    $scope.clear=function(){
        $scope.message="";
    };
    $scope.save=function(){
        alert("file got saved");
    };
});

then add the angular script tag inside the body or head tag

<!DOCTYPE html>
<html>

  <head>
    <script data-require="angular.js@1.5.8" data-semver="1.5.8" src="https://code.angularjs.org/1.5.8/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body ng-app="notePad" ng-controller="notePadCtrl">
    <textarea ng-model="message"></textarea>
    <button ng-click="save()">save</button>
    <button ng-click="clear()">clear</button>
  </body>

</html>

check out the pluker

Comments