2015-11-17 3 views
3

Вот мой код, как получить данные списка, используя rest api в AngularJS. Здесь у меня проблема, я не могу связать данные списка в sp. Я обнаружил, что контроллер нельзя вызвать.Как получить данные списка в RestApi с помощью angularjs

<pre lang="Javascript"> 
 

 
var myAngApp = angular.module('SharePointAngApp', []); 
 
alert('sss'); 
 
myAngApp.controller('spCustomerController', function ($scope, $http) { 
 

 
    $http({ 
 
     method: 'Post', 
 
     url: appWebUrl + "/_api/SP.AppContextSite(@TargetSite)/web/lists/getByTitle('InfoList')/[email protected]='" + targetSiteUrl + "'", 
 
     headers: { "Accept": "application/json;odata=verbose" } 
 
    }).success(function (data, status, headers, config) { 
 
     $scope.customers = data.d.results; 
 
    }).error(function (data, status, headers, config) { 
 
    }); 
 

 
    $(document).ready(function() { 
 
     var appWebUrl = ""; 
 

 
     var targetSiteUrl = ""; 
 
     var ready = false; 
 
     var params = document.URL.split("?")[1].split("&"); 
 
     for (var i = 0; i < params.length; i = i + 1) { 
 
      var param = params[i].split("="); 
 
      switch (param[0]) { 
 
       case "SPAppWebUrl": 
 
        appWebUrl = decodeURIComponent(param[1]); 
 
        break; 
 
       case "SPHostUrl": 
 
        targetSiteUrl = decodeURIComponent(param[1]); 
 
        break; 
 
      } 
 
     } 
 
     // load the request executor script, once ready set a flag that can be used to check against later 
 
     $.getScript(appWebUrl + "/_layouts/15/SP.RequestExecutor.js", function() { 
 
      ready = true; 
 
     }); 
 
    }); 
 

 
}); 
 

 

 
</pre>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<pre lang="HTML"> 
 
    <div ng-app="SharePointAngApp" class="row"> 
 
     <div ng-controller="spCustomerController" class="span10"> 
 
      <table class="table table-condensed table-hover"> 
 
       <tr> 
 
        <th>Title</th> 
 
        <th>Employee</th> 
 
        <th>Company</th> 
 

 
       </tr> 
 
       <tr ng-repeat="customer in customers"> 
 
        <td>{{customer.Title}}</td> 
 
        <td>{{customer.Employee}}</td> 
 
        <td>{{customer.Company}}</td> 
 
       </tr> 
 
      </table> 
 
     </div> 
 
    </div> 
 
</pre>

+0

сделать вас увидеть ошибки в консоли? – koox00

+0

Да ..! Я нашел ошибку «appWebUrl не определен» – Lier

ответ

1

$ .getScript должен закончить, а затем вызвать $ HTTP, и вы должны пройти ВАР для URLs. взгляд на эту

var myAngApp = angular.module('SharePointAngApp', []); 
myAngApp.controller('spCustomerController', function ($scope, $http, $location) { 
    var params = $location.search(); 
    var appWebUrl = params.SPAppWebUrl; 
    var targetSiteUrl = params.SPHostUrl; 
    var ready_check = false; 

    // this has to finish and then call getData 
    ready().then(getData); 

    function ready() { 

     // load the request executor script, once ready set a flag that can be used to check against later 
     return $.getScript(appWebUrl + "/_layouts/15/SP.RequestExecutor.js", function() { 
      ready_check = true; 
     }) 

    } 

    function getData() { 
     return $http({ 
      method: 'POST', 
      url: appWebUrl + "/_api/SP.AppContextSite(@TargetSite)/web/lists/getByTitle('InfoList')/[email protected]='" + targetSiteUrl + "'", 
      headers: { "Accept": "application/json;odata=verbose" } 
     }).success(function (data, status, headers, config) { 
      $scope.customers = data.d.results; 
     }).error(function (data, status, headers, config) { 
     }); 

    } 

}); 

Вы должны взглянуть на этот $location, может быть полезным.

+0

Спасибо за ваш добрый повтор ..! Я пробовал ур код, но даже его не работает. Найдена новая ошибка «targetUrl не определен» – Lier

+0

Я ошибся, это должно быть 'targetSiteUrl'. попробуйте сейчас – koox00

+0

можно проверить с кодом, как только agian нашел ту же проблему – Lier

0

Наконец найдено решение, но есть небольшая проблема, что умеет читать только первую columan что название остальные пункты cannont быть зацикленной проверить URL в $ HTTP

var appWebUrl, targetSiteUrl; 
 
var ready = false; 
 
var myAngApp = angular.module('SharePointAngApp', []); 
 
myAngApp.controller('spCustomerController', function ($scope, $http) { 
 
    var params = document.URL.split("?")[1].split("&"); 
 
    for (var i = 0; i < params.length; i = i + 1) { 
 
     var param = params[i].split("="); 
 
     switch (param[0]) { 
 
      case "SPAppWebUrl": 
 
       appWebUrl = decodeURIComponent(param[1]); 
 
       break; 
 
      case "SPHostUrl": 
 
       targetSiteUrl = decodeURIComponent(param[1]); 
 
       break; 
 
     } 
 
    } 
 
    // load the request executor script, once ready set a flag that can be used to check against later 
 
    $.getScript(appWebUrl + "/_layouts/15/SP.RequestExecutor.js", function() { 
 
     ready = true; 
 
    }); 
 
    $http({ 
 
     method: 'Get', 
 
     url: appWebUrl + "/_api/SP.AppContextSite(@TargetSite)/web/lists/getByTitle('InfoList')/[email protected]='" + targetSiteUrl + "'", 
 
     //url: appWebUrl + "/_api/SP.AppContextSite(@TargetSite)/web/lists/getByTitle('InfoList')/items?$Select=Title,Employee,Company&@TargetSite='" + targetSiteUrl + "'", 
 
     headers: { "Accept": "application/json;odata=verbose" } 
 
    }).success(function (data, status, headers, config) { 
 
     $scope.customers = data.d.results; 
 
    }).error(function (data, status, headers, config) { 
 
    }); 
 
});

Смежные вопросы