0

I'm having a hard time passing data to a controller. My scenario is that I have a function that gets a json object and I need to pass that object to a controller that does the rest of the work. Below is what I'm working with:

Getting the data before DOM loads and save it in the Data variable.

    $.ajax({
    url: "/_api/web/lists/getbytitle('Consultant%20Profile')/items?$filter=ID%20eq%20"+curItemId,
    type: "GET",
    headers: {"Accept": "application/json;odata=verbose"},
    success: function(data){
        Data = data.d.results; //global variable that I want to pass.
        console.log(Data);

    },
    error: function(data){
        console.log("something is not right with - ", data)
    }   
   });

This is the controller that I would like to pass Data to.

var app = angular.module("contactApp",[]);
app.value('listData', Data);
app.controller('ContactController',['listData','$scope','$http',function(listData,$scope,$http){
        console.log(listData);
        //Do something here...  
});

I'm trying to use value() but getting an error if I pass in anything other than a string. So if Data = "Sometext" it would console out that text but if it's an object it doesn't work. Not sure what I'm doing wrong but is there a better way of doing this!?

1
  • 1
    why don't you use the $http service from angular? Commented Mar 6, 2015 at 14:57

1 Answer 1

1

Yup as Rafael has mentioned try using the $http service from Angular

https://docs.angularjs.org/api/ng/service/$http

Sign up to request clarification or add additional context in comments.

3 Comments

I can get the data but I can't pass it. Angular doesn't like the data $ajax returns!?
what error are you getting? what type of var are you trying to pass?
I think I got it! The call is async so the variable was not defined when the controller was fired off (It takes the call longer to return data, same behavior using $http). I'm exploring angular's factory to get the data and then inject it to the controller.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.