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

时间:2015-05-20 12:37:15

标签: javascript angularjs sharepoint sharepoint-2013 sharepoint-api

我在AngularJS应用程序中使用SharePoint的JavaScript对象模型,并且需要在页面加载时执行其中一个函数,以便填充数组并由ng-repeat使用。

目前,它在页面加载时向控制台记录数组push,但似乎没有将值提交到数组中以便在页面/ $ scope上使用,直到我单击/然后输出输出框,单击OK在一个警告框,或一个让我在页面上的虚拟链接。这是我很困惑的原因,为什么它可以在加载时正确地记录数组,但没有在页面的$ scope中准备好数组。

我尝试了以下内容,但没有成功:

1)我已将调用执行函数移至控制器底部

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

3)我尝试将其包装在以下函数中:

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

我不明白为什么下面这个调用REST服务的内容在页面加载时执行/通过推入$ scope用于ng-repeat而完美地工作,而我的函数并没有这样做。 T:

 // 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中执行的,还是其他什么?

1 个答案:

答案 0 :(得分:0)

我能够让这个工作。问题是Angular似乎不尊重非Angular函数,因此使用$ scope.apply将数组推送到$ 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());
            });
          };