Restangular调用REST服务导致TypeError:无法设置null的属性'route'

时间:2015-11-13 08:45:40

标签: angularjs rest

我在AngularJS应用程序中使用restangular来从REST Servives中获取记录。

我正在调用REST服务,如下所示:

var c=[1,2];
var z,y,x,w,a,b;
function mj1(){
    mj2();
    document.getElementById("showup").innerHTML='????';//<- which one? i don't know how to choose the variable 
}
function mj2(){
    username='michel jackson';
    for(a=1;a<=c.length;a++){
        z="<div id='bigbox'>"+
        for(b=1;b<=c.length;b++){
            y[b]="<div class='colkiri'>"+
                "<div class='colkirichild'></div>"+
            "</div>";
        }
        x="<div class='colkanan'>"+
                "<div class='colkananchild'>"+
                    "<div id='username'>"+
                        "<span class='usernamechild'><img src='http://images2.fanpop.com/images/photos/7600000/Bad-michael-jackson-7647469-1787-2560.jpg' width='15' height='15' class='bayangan'></img> username</span> <span class='countdown'>5 minutes ago</span>"+
                    "</div>"+
                    "<div id='comment'>"+
                        "<div class='commentchild'>Michael Joseph Jackson was born on August 29, 1958. He was the eighth of ten children in an African-American working-class family who lived in a two-bedroom house on Jackson Street in Gary, Indiana, an industrial city and a part of the Chicago metropolitan area.[12][13] His mother, Katherine Esther Scruse, was a devout Jehovah's Witness.</div>"+
                        "<div id='reportthis'>"+
                            "<span id='idreply' onclick='clickreply(this)'>reply</span>"+
                        "</div>"+
                    "</div>"+
                "</div>"+
            "</div>"+
        "</div>";
    }
}

但是,我收到此错误

$scope.students = function()
{
    $scope.studentsGet = ListService.getList($cookieStore.get('baseUrl'), 'students/reference/1', null,null);
    $scope.studentsGet.then(function(data) {
    $scope.students = data.list;
  });
}

导致问题的地方。帮助我。

1 个答案:

答案 0 :(得分:0)

我认为你错误地使用getList()方法。我不确定您的ListService究竟是什么,但我认为它是自定义的Restangular服务(https://github.com/mgonto/restangular#how-to-create-a-restangular-service-with-a-different-configuration-from-the-global-one

在这种情况下,您应该在服务配置中设置基本路径(而不是在getList()方法内:

app.factory('ListService', function(Restangular) {
  return Restangular.withConfig(function(RestangularConfigurer) {
    RestangularConfigurer.setBaseUrl($cookieStore.get('baseUrl'));
  });
}); 

然后,在控制器中注入ListService之后,您可以使用以下内容:

$scope.studentsGet = function() {
    ListService.all('students/reference/1').getList().then(function(data){
         $scope.students = data.list;
    });
}

所以,首先你必须创建Restangular对象,这是一个指向元素或集合的指针(这是oneall方法分别为它们传递{{1参数)。然后你可以在那个对象上调用route

修改 如果你的getList()是解耦的Restangular Service(https://github.com/mgonto/restangular#decoupled-restangular-service),这意味着它已经是一个Restangular对象,你可以调用ListService方法。

getList()