2016-09-02 38 views
2

我正在構建一個應用程序以從Moodle Web服務獲取Json數據,並使用AngularJs在應用程序中顯示數據。 Moodle web服務有多種功能,所以我需要Angular應用程序中的多個控制器。獲取Angular與Moodle webservice配合工作

我正在使用Visual Studio和Cordova編寫應用程序。

我想出了一個解決方案,用於從Moodle獲取令牌,使用jstorage存儲它,並將其顯示在單頁移動應用程序的各個窗格上。

感謝許多其他的StackOverflow答案,我已經習慣了這個解決方案!

(這是那些「問你的問題和回答的自己」的帖子一個 - 但進一步的建議,歡迎)

參見 - 從那裏你Authenticate with Moodle from a mobile app

回答

3

步驟1.使用網絡令牌它存儲在jstorage(在myApp.js)

(見Authenticate with Moodle from a mobile app如何存儲會話令牌)

moodleUrl = 'https://moodle.yourwebsite.com/webservice/rest/server.php'; 
session = $.jStorage.get('session', ''); // syntax: $.jStorage.get(keyname, "default value") 
moodlewsrestformat = 'json'; 
wstoken = session; 
concatUrl = moodleUrl + '?moodlewsrestformat=' + moodlewsrestformat + '&wstoken=' + wstoken + '&wsfunction='; 

步驟2.創建角模塊(myApp.js)

angular.module('myApp.controllers', []); 

var myApp = angular.module('myApp', ['ui.bootstrap']); 

步驟3.創建(在ui.bootstrap部分取決於是否使用自舉UI元素是可選的)控制器(在myApp.js)

myApp.controller('myFirstCtrl', function ($scope, $http) { 
    url = concatUrl + 'local_appname_ws_function_name'; 

    $http.get(url).then(function (response) { 
     $scope.items = response.data; 
    }) 
}); 

myApp.controller('mySecondCtrl', function ($scope, $http) { 
    url = concatUrl + 'local_appname_ws_function_name'; 

    $http.get(url).then(function (response) { 
     $scope.things = response.data; 
    }) 
}); 

步驟4:在HTML創建NG-應用實例(在你莫的index.html膽汁應用)

<body> 
    <div class="overlay">&nbsp;</div> 
    <div data-role="page" id="welcome-page"> 
     <div data-role="header" class="header"> 
      <h1 id="app-title"> 
       App title 
      </h1> 
     </div> 
     <div role="main" id="main" class="ui-content scroll" ng-app="myApp"> 
    <!--ng-repeat elements will go here--> 
</div> 

步驟5.對每個控制器創建一個NG-重複元件(index.html中)

<div role="main" id="main" class="ui-content scroll" ng-app="myApp"> 
    <div data-role="content" class="pane" id=""> 
     <h2>Your Items</h2> 
     <div class="page-wrap scroll" ng-controller="myFirstCtrl"> 

      <div ng-repeat="item in items | orderBy : 'item.name'" id="{{item.id}}"> 
       <div class="item-data"> 
        {{item.name}}<br /> 
        <time datetime="{{item.date}}">{{item.date}}</time> 
       </div> 
      </div> 
     </div> 
    </div> 
    <div data-role="content" class="pane" id=""> 
     <h2>Your Things</h2> 
     <div class="page-wrap scroll" ng-controller="mySecondCtrl"> 

      <div ng-repeat="thing in things | orderBy : 'thing.name'" id="{{thing.id}}"> 
       <div class="thing-data"> 
        {{thing.name}}<br /> 
        <time datetime="{{thing.date}}">{{thing.date}}</time> 
       </div> 
      </div> 
     </div> 
    </div> 
</div> 
+0

NB:碼被提供 '是',但無任何承諾它適合用途,也提供沒有任何承諾維護或支持。 –