无法在angularjs中调用ng-click?

时间:2014-09-09 17:25:29

标签: javascript angularjs spring rest angularjs-ng-click

我是一名新手并且正在学习AngularJS,我陷入困境,无法弄清楚我在做什么。所以我有一个Spring RESTful服务,它工作正常,但我无法在ng-click上调用它。这是我的代码片段

的index.jsp

    <body>
    <div data-ng-controller="Controller1">
  <p>Create a new user:</p>
  <p>
    Name: <input data-ng-model="name" />
  </p>
  <button data-ng-click="createPerson()" >add</button>

  <p data-ng-show="newUserId">
    User created with id: {{newUserId}}
  </p>
</div>
</body>

我的app.js

    var demoApp=angular.module('demoApp',['ngRoute']);
    demoApp.controller('Controller1', function ($scope,$http) {
    console.log("In controller");
    var urlBase="http://localhost:8080/SpringAngularMaven/";
    $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";


    $http.createPerson = function createPerson() {
        $http.get('http://localhost:8080/SpringAngularMaven/rest/emp/dummy').
        success(function(data) {
            $scope.tasks = data;
            console.log(data);
            $scope.newUserId = data;

        });
    };

输出 因此,当加载页面时,只执行console.log("In controller");,当我点击按钮时没有任何事情发生....

1 个答案:

答案 0 :(得分:1)

看起来您的控制器中有拼写错误。您需要将createPerson函数绑定到$scope

   var demoApp=angular.module('demoApp',['ngRoute']);
    demoApp.controller('Controller1', function ($scope,$http) {
    console.log("In controller");
    var urlBase="http://localhost:8080/SpringAngularMaven/";
    $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";

    //Changed from $http.createPerson to $scope.createPerson
    $scope.createPerson = function() {
        $http.get('http://localhost:8080/SpringAngularMaven/rest/emp/dummy').
        success(function(data) {
            $scope.tasks = data;
            console.log(data);
            $scope.newUserId = data;

        });
    };