0

I am building an application that has a button that loads up help based on IDs from the JSON, when I have the JSON in the controller.js I see it on the web page but if I do it externally I do not see anything. I feel I am missing something extremely simple but may be over looking it. Any ideas?

controllerNEW.js

app.factory("UserService", function($rootScope, $http){
     function getFile(){
       return $http.get('stat.json').then(function(data) {
            return data;
        }); 
    } 

    return{statErr: getFile,

        //hide the displayed tooltip based on its id
        hideTooltip: function(key, scopes, data)
        {

controllerOLD.js

app.factory("UserService", function($rootScope){
    return{
        statErr: [
            {
                selector: "#userEmail",
                fieldTitle: "Email",
                placement: "right",
                content: "test",
                offsetTop: 500,
                correctKey: "@test",
                inErrorList: false
            },
            {
                selector: "#userId",
                fieldId: "id1",
                fieldTitle: "ID",
                placement: "right",
                content: " number should contain 4 zeroes '...0000...'",
                offsetTop: 500,
                correctKey: "0000",
                inErrorList: false
            }
        ],

        //hide the displayed tooltip based on its id
        hideTooltip: function(key, scopes, data)
        {
4
  • To see the response of your data, the callback is data.data Commented Feb 22, 2018 at 15:42
  • function(data) { return data; } is just returning data... into oblivion. You're not treating it and not doing anything with it. Commented Feb 22, 2018 at 15:42
  • I am calling the function which I expected would return the data Commented Feb 22, 2018 at 15:44
  • Another change could just leave the request in your service and execute the promise in your controller. In fact, that is the good practice. Commented Feb 22, 2018 at 15:45

4 Answers 4

1

The issue is getFile() returns a promise not the data itself. It is true that you have a .then(function(data) {return data; });, but, .then() also returns a promise.

So to fix this issue, in your actual code, after you call getFile() you also need to add .then() to have access to your data:

getFile().then(function(data){
 // now you have access to your data
})
Sign up to request clarification or add additional context in comments.

Comments

0

you need to access the data object in the response object.

example:

var jsonResponse = $http.get('content.json').then(function(response) {
        return response.data;
    });

1 Comment

when you "console.log" the response, what do you see?
0

In your example, data is the whole response to your request, including headers and status. To see your data from the 'stat.json' file, you would access the data object of your data. So, the change is.

function getFile(){
       return $http.get('stat.json').then(function(data) {
            return data.data;
        }); 
    } 

1 Comment

please, show me the controller where you are calling the service. I don't see where is the implementation
0

you can try something like this

 $http.get('stat.json').then(function(result){
     $scope.dataset = result.data
     console.log($scope.dataset)
    },function(error){
     console.log(error)
    })

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.