在AngularJS应用程序页面加载时执行SharePoint函数($scope issue??)

Execute SharePoint Function On Page Load in AngularJS Application ($scope issue???)

本文关键字:函数 issue SharePoint scope 执行 应用程序 AngularJS 加载      更新时间:2023-09-26

我在一个AngularJS应用程序中使用SharePoint的JavaScript对象模型,需要它的一个函数在页面加载时执行,这样数组就会被ng-repeat填充和使用。

目前,它在页面加载时将数组推送记录到控制台,但似乎不会将值提交到数组中以供在page/$作用域上使用,直到我单击输入框中的/然后退出,在警告框中单击OK,或者在页面上单击虚拟链接。这就是我感到困惑的地方,为什么它可以在加载时正确地记录数组,但没有在$作用域中为页面准备好数组。

我尝试了以下操作,但没有成功:

1)我已经将执行函数的调用移到了控制器

的底部

2)我试过了angular.element(document).ready

3)我试过把它包装在一个函数中,如:

$scope.initTaxonomy = function () {
        $(function () {
        // Function here
        });
        };

我不明白的是,为什么以下调用REST服务的代码在页面加载时执行/通过推入$scope以在ng-repeat中使用而完美地工作,而我的函数却没有:

 // Array holding items for display on homepage
    $scope.items = [];
    appItems.query(function (result) {
        // Data is within an object of "value", so this pushes the server side array into the $scope array
        var result = result.value;
        var userInfo;
        // Foreach result, push data into items array
        angular.forEach(result, function (resultvalue, resultkey) {
            $scope.items.push({
                title: resultvalue.Title,
                status: resultvalue.Status
             });
        });
    });

这个函数将在页面加载时成功地记录到控制台,但是直到我在页面上单击周围时才填充我的ng-repeat中的数组:

    var termsArray = [];
    $scope.termsArray = termsArray;
    execOperation();
    function execOperation() {
        //Current Context
        var context = SP.ClientContext.get_current();
        //Current Taxonomy Session
        var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
        //Term Stores
        var termStores = taxSession.get_termStores();
        //Name of the Term Store from which to get the Terms.
        var termStore = termStores.getByName("Taxonomy_1111");
        //GUID of Term Set from which to get the Terms.
        var termSet = termStore.getTermSet("1111111");
        var terms = termSet.getAllTerms();
        context.load(terms);
        context.executeQueryAsync(function () {
            var termEnumerator = terms.getEnumerator();
            while (termEnumerator.moveNext()) {
                var currentTerm = termEnumerator.get_current();
                var guid = currentTerm.get_id();
                var guidString = guid.toString();
                termsArray.push({
                    termName: currentTerm.get_name(),
                    termGUID: guidString,
                });
                //getLabels(guid);
            }
            console.log($scope.termsArray)
        }, function (sender, args) {
            console.log(args.get_message());
        });
       };

我的控制器通过app.js加载页面,如下所示:

$routeProvider.
            when('/', { templateUrl: '/views/home.html', controller: 'appHomeItemsCtrl' }).
            when('/add-item', { templateUrl: '/views/add-item.html', controller: 'appItemPostCtrl' }).
            otherwise({ redirectTo: '/' });  

这是一个问题与SharePoint的JSOM需要正确执行的东西,我是如何试图在AngularJS执行,还是别的什么?

我能够让它工作。问题是Angular似乎不尊重非Angular函数,所以使用$scope。将我的数组推入$scope是有效的。注意,包装整个函数会导致Angular摘要出现问题:

    var termsArray = [];
    execOperation();
    function execOperation() {
            //Current Context
            var context = SP.ClientContext.get_current();
            //Current Taxonomy Session
            var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
            //Term Stores
            var termStores = taxSession.get_termStores();
            //Name of the Term Store from which to get the Terms.
            var termStore = termStores.getByName("Taxonomy_1111");
            //GUID of Term Set from which to get the Terms.
            var termSet = termStore.getTermSet("1111");
            var terms = termSet.getAllTerms();
            context.load(terms);
            context.executeQueryAsync(function () {
                var termEnumerator = terms.getEnumerator();
                while (termEnumerator.moveNext()) {
                    var currentTerm = termEnumerator.get_current();
                    var guid = currentTerm.get_id();
                    var guidString = guid.toString();
                    termsArray.push({
                        termName: currentTerm.get_name(),
                        termGUID: guidString,
                    });
                }
                console.log(termsArray)
                $scope.$apply(function () {
                    $scope.termsArray = termsArray;
                });
            }, function (sender, args) {
                console.log(args.get_message());
            });
          };