简体   繁体   English

将异步REST调用JSON添加到Angular $ scope

[英]Add Async REST Call JSON to Angular $scope

I need some help understanding how to structure an AngularJS controller. 我需要一些帮助,以了解如何构造AngularJS控制器。

The controller is to execute an Async REST call to SharePoint and put the JSON results into a $scope.SearchResults property. 控制器将执行对SharePoint的异步REST调用,并将JSON结果放入$ scope.SearchResults属性。

The Async REST call is executed by the getDataSP() function and the $scope.SearchResults is assigned in the onQuerySuccess() callback. 异步REST调用由getDataSP()函数执行,并且$ scope.SearchResults在onQuerySuccess()回调中分配。 The onQuerySuccess() method has the statement onQuerySuccess()方法具有以下语句

console.log('Search Results Returned.' + $scope.SearchResults);

which does display the expected "[object Object]" so I know it is returning something. 它确实显示了预期的“ [object Object]”,所以我知道它正在返回某些内容。

However the HTML page has the following tag to show the JSON object. 但是,HTML页面具有以下标记以显示JSON对象。 I also expect it to display "[object Object]" but it never shows up! 我也希望它显示“ [object Object]”,但它永远不会显示!

<span >{{SearchResults}}<span>

Here is the code for the entire module. 这是整个模块的代码。

(function () {
    var app = angular.module('FormApp', []);

    app.controller('FormController', ['$scope', function ($scope) {

        var context = SP.ClientContext.get_current();
        var user = context.get_web().get_currentUser();

        function onQuerySuccess() {
            if (results && results.m_value && results.m_value.ResultTables && results.m_value.ResultTables[0] && results.m_value.ResultTables[0]) {
                $scope.SearchResults = results.m_value.ResultTables[0];
                console.log('Search Results Returned.' + $scope.SearchResults);
            } else {
                results = {};
                console.log('Search Results Failed');
            }
        }

        function onQueryFail(sender, args) {
            console.log('Query failed. Error:' + args.get_message());
        }

        function getDataSP() {


            var keywordQuery = new Microsoft.SharePoint.Client.Search.Query.KeywordQuery(context);
            keywordQuery.set_queryText("*");
            keywordQuery.set_sourceId("d0aa2c2e-7709-46f1-835a-244f483e4b0e");

            var managedProperties = ['RefinableString00'];
            var kwqProperties = keywordQuery.get_selectProperties();
            for (var i = 0; i < managedProperties.length; i++) {
                kwqProperties.add(managedProperties[i]);
            }

            var searchExecutor = new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(context);
            results = searchExecutor.executeQuery(keywordQuery);

            context.executeQueryAsync(onQuerySuccess, onQueryFail)

        };



        getDataSP();


        $scope.ProjectSelected = function (proj) {
            $scope.SelectedProj = proj;
        };

    }
    ]);


    angular.bootstrap(document.body, ['FormApp']);

})();

you need to notify Angular that you changed the model. 您需要通知Angular您更改了模型。 Usually when your code is triggered or the callback handled by Angular, it checks automatically, but in this case, it doesn't know anything about your callback. 通常在触发代码或由Angular处理回调时,它会自动检查,但在这种情况下,它对回调一无所知。 You can do this simply by calling $scope.$apply() at the end of your callback. 您只需在回调的末尾调用$scope.$apply()即可完成此操作。

some more info: https://github.com/angular/angular.js/wiki/When-to-use- $scope.$apply() 一些更多信息: https : //github.com/angular/angular.js/wiki/When-to-use- $ scope。$ apply()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM