无法达到角度方法

时间:2016-01-03 00:20:44

标签: javascript angularjs

嘿伙计们出于某种原因我的代码在下面工作并给我这个结果。当我点击保存按钮时,窗口中没有任何警报?我想某种程度上我没有达到角度库但我不确定。帮助将不胜感激!三江源!

enter image description here

的index.html:

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

<head>
<link rel="stylesheet" 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="angular.js@1.4.x" 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)
  }
}

1 个答案:

答案 0 :(得分:2)

你需要ng-app和ng-controller,这应该可行

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