0

I am calling restful service from AngularJS. HTML is very basic with a input text box and a button for query.

// basic.html

<!DOCTYPE html>
<html ng-app="cgApp" >
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular-resource.js"></script>
    <script src="../js/controller.js"></script>
    <script src="../js/service.js"></script>

  </head>
  <body>
    <div ng-controller="CgseqCtrl">
        <input ng-model="analysisid"><button ng-click="searchById()">Search</button>
        <table>
            <tr>
                <td>{{seq.analysisId}}</td>
                <td>{{seq.center}}</td>
                <td>{{seq.diseaseAbbr}}</td>
                <td>{{seq.filepath}}</td>
                <td>{{seq.library}}</td>
            </tr>
        </table>        
    </div>
  </body>
</html>

I use a service to call rest api

// service.js
app.factory("Cgseq", function ($http) {
    // return $resource('http://localhost:8080/cgweb/api/seqs/fdebfd6e-d046-4192-8b97-ac9f65dc2009');
    var service = {};
    service.getSeqById = function(analysisid) {
        return http.get('http://localhost:8080/cgweb/api/seqs/' + analysisid);
    }


    service.getSeq = function() {
        return $http.get('http://localhost:8080/cgweb/api/seqs/fdebfd6e-d046-4192-8b97-ac9f65dc2009');
    }

    return service;
});

The function searchById() will be executed once the button is clicked. It is implemented in my controller.

// controller.js
var app = angular.module('cgApp', [])

app.controller('CgseqCtrl', ['$scope', 'Cgseq', function($scope, Cgseq){
    $scope.searchById() = function() {
        CgSeq.getSeqById($scope.analysisid)
        .then(function(response){
            $scope.seq = response;
        });
    }
}]);

When I load basic.html in a browser, even before I type in something in the input box and click the button, I got the following error:

angular.js:12416 TypeError: $scope.searchById is not a function
    at new <anonymous> (controller.js:8)
1
  • 1
    You try using $scope.searchById = function() {....} Hopefully this will solve your issue. Commented Apr 7, 2016 at 3:26

1 Answer 1

2

You should remove the () from $scope.searchById() = function

And correct the typo (case-sensitivity) for Cgseq I.e.:

$scope.searchById = function() {
    Cgseq.getSeqById($scope.analysisid)
    .then(function(response){
        $scope.seq = response;
    });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.