angularjs - 让 Angular 与 Moodle Web 服务配合使用

标签 angularjs json single-page-application moodle-api jstorage

我正在构建一个应用程序来从 Moodle Web 服务获取 Json 数据,并使用 AngularJs 在应用程序中显示数据。 Moodle Web 服务上有多种功能,因此我需要在 Angular 应用程序中使用多个 Controller 。

我正在使用 Visual Studio 和 Cordova 来编写应用程序。

我提出了一个解决方案,用于从 Moodle 获取 token ,使用 jstorage 存储它,并将其显示在单页移动应用程序的各个 Pane 上。

感谢我使用过的许多其他 StackOverflow 答案来得出此解决方案!

(这是“提出你的问题并自己回答”的帖子之一 - 但欢迎进一步的建议。)

另请参阅 - Authenticate with Moodle from a mobile app

最佳答案

第 1 步:从 jstorage 中存储 Web token 的位置(在 myApp.js 中)获取 Web token

(有关如何存储 session token 的信息,请参阅 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 步:创建 Angular 模块(在 myApp.js 中)

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

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

(ui.bootstrap 部分是可选的,具体取决于您是否使用 Bootstrap UI 元素)

第 3 步:创建 Controller (在 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-app 实例(在移动应用的 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 步:为每个 Controller 创建一个 ng-repeat 元素(在 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>

关于angularjs - 让 Angular 与 Moodle Web 服务配合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39287594/

相关文章:

javascript - AngularJS:如何使用 $filter 从对象数组中查找对象,给定数组中的属性值数组

java - Android 数组的空指针

javascript - ng-show 令人不安的 div 布局 - angularJS

javascript - 翻译单页应用程序

javascript - Ng-Show 多次调用

javascript - 奇怪的语法 : invoking a javascript function with two parenthesis pairs

java - com.android.volley.parse 错误 org.json.jsonexception java.lang.string 类型的值无法在 Android Volley 中转换为 jsonArray

java - android 使用 jsontokener 解析 json 对象

c# - 在 SPA 应用程序的 ASP.NET 5 中正确使用 AntiForgery token ?

javascript - AngularJS改变状态问题