步驟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"> </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>
NB:碼被提供 '是',但無任何承諾它適合用途,也提供沒有任何承諾維護或支持。 –