Ad

Cant Reach The Angular Method

- 1 answer

Hey guys for some reason my code below dosent work and gives me this result. And when i click the save button, nothing alerts in the window? I think somehow im not reaching the angular library but im not sure. Help would be appreciated! Thankyou!

enter image description here

index.html:

<!DOCTYPE html>
<html lang="en">

<head>
<link rel="stylesheet" target="_blank" rel="nofollow noreferrer" target="_blank" rel="nofollow noreferrer" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
<script data-require="[email protected]" src="https://code.angularjs.org/1.4.8/angular.js" data-semver="1.4.8"></script>
    <script src = "https://rawgit.com/nirus/Angular-Route-Injector/master/dist/routeInjector.js"></script>
    <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular-route.js"></script>
    <script type="text/javascript" src="main.js"></script>

    <script type="text/javascript" src="main.js"></script>
</head>
<body style="margin:20px">
<div>
    <label>Employee Name</label>
    <input type = "text" id="employeeName" ng-model="employeeName"><br />

    <label>Emploee Age</label>
    <input type = "text" id="employeeAge" ng-model="employeeAge"><br />
  </div>
  <div>
    <button class = "btn btn-default" ng-click = "saveEmployee()">
      Save <span class = "glyphicon glyphicon-ok">
    </button>

  </div>

  <div>
      Emplyee Name: {{employeeName}}<br />
      Employee Age: {{employeeAge}}
  </div>
 </body>

</html>

main.js:

function employeeCtrl($scope){
  $scope.employeeName = ""
  $scope.employeeAge = 0;
  $scope.saveEmployee = function(){
    alert('You have saved ' + $scope.employeeName)
  }
}
Ad

Answer

You need ng-app and ng-controller, this should work

<body ng-app="" ng-controller="employeeCtrl" style="margin:20px">
  <div>
    <label>Employee Name</label>
    <input type = "text" id="employeeName" ng-model="employeeName"><br />

    <label>Emploee Age</label>
    <input type = "text" id="employeeAge" ng-model="employeeAge"><br />
  </div>
  <div>
    <button class = "btn btn-default" ng-click = "saveEmployee()">
      Save <span class = "glyphicon glyphicon-ok">
     </button>

</div>

  <div>
      Emplyee Name: {{employeeName}}<br />
      Employee Age: {{employeeAge}}
  </div>
</body>
Ad
source: stackoverflow.com
Ad