0

Json Data

   {
  "TotalRecordCount": 50,
  "Results": [
    {
      "TestId": 5002,
      "TestName": "Test 01/05/2016"
    }
  ]
}

My Service

 function getTestSchedule() {
        var deferred = $q.defer();

        $http.get(TestAppConfig.TestApiRoot + "/Tests/gettestschedule/").then(function (response) {
            deferred.resolve(response);
        }, function (resp) {
            deferred.reject(resp);
        });

        return deferred.promise;
    };

My Controller Code

TestVm.test = TestModel.getTestSchedule;

So here controller is doing http call to web api, instead of http call how can I bind the page with the above json data

1 Answer 1

2

I'm not sure I understood your question correctly but why don't you do:

TestModel.getTestSchedule().then(function(result) { TestVm.test = result; });

Maybe you'll have to replace result by result.data. Besides, your service is a bit weird, why don't you just do :

function getTestSchedule() {
    return $http.get(TestAppConfig.TestApiRoot + "/Tests/gettestschedule/");
};

Your code just creates a Promise mimicking the one returns by $http.get so you could simply return the one of $http.get.

EDIT

See this plunker: http://plnkr.co/edit/5IqDNDoN4ewb5klKnzeU

EDIT 2

Finally, according to the discussion below, the expected answer was simply:

TestVm.test = { 
    "TotalRecordCount": 50, 
    "Results": [ ... ] 
}
Sign up to request clarification or add additional context in comments.

7 Comments

my question was how to dump json data into angular js scope variable TestVm.test = json data
Have a look at this: plnkr.co/edit/5IqDNDoN4ewb5klKnzeU the data variable contains the JSON contained in the test.json file, now you can do whatever you want with it ;-)
can I store json data in a variable and then use it in TestVm.test = json variable instead of using angular service and json file
Well... It depends on what exactly you want to do. You can store value at application level via the function .constant on your module, you can store hard coded data in a service (like the example above except that you can directly return the object instead of a promise), you can also store the hard coded value directly in your controller. Without more information, I can't tell you what you should do ;-)
well my requirement is simple i want to use json data into that controller scope variable to have a quick test on my side how page looks...I am not worried about creating constants or anything. Because after my test i am going to undo my changes
|

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.