执行功能的SharePoint页面加载在AngularJS应用($范围的问题???) [英] Execute SharePoint Function On Page Load in AngularJS Application ($scope issue???)

查看:172
本文介绍了执行功能的SharePoint页面加载在AngularJS应用($范围的问题???)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用SharePoint的JavaScript对象模型在AngularJS应用程序,并需要它的功能在页面加载执行这样一个数组填充并通过NG-重复使用的一种。

I am using SharePoint's JavaScript Object Model in an AngularJS app and need one of its functions to execute on page load so an array is populated and used by an ng-repeat.

目前,它会记录到控制台上的页面加载阵推,但直到我点击/再出一个输入框,单击确定不会出现在值提交到阵列上页/ $范围内使用在一个警告框,或者说,让我在页面上的虚拟链接。这是我很困惑,为什么它可以正确登录阵列上的负载,而不是在阵列中准备对页面$范围。

Currently, it logs to the console the array push on page load, but does not appear to commit the values into the array for use on the page/$scope until I click in/then out of a input box, click OK on an alert box, or a dummy link that keeps me on the page. This is where I am confused as to why it can log the array properly on load, but not have the array ready in $scope for the page.

我曾尝试以下,但没有成功:

I have tried the following, but without success:

1)我提出的呼叫执行该函数到控制器的底

1) I have moved the call to execute the function to the bottom of the controller

2)我已经试过angular.element(文件)。就绪

2) I have tried angular.element(document).ready

3)我曾尝试在一个函数进行包装,如:

3) I have tried wrapping it in a function such as:

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

我不明白的是为什么以下,其中调用REST服务,在页面加载执行/推到$余地在NG-重复使用的作品完美,而我的功能不会:

What I don't understand is why the following, which calls to a REST service, executes on page load/works perfectly by pushing into $scope for use in an ng-repeat, while my function doesn'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-重复阵列:

This is the function that will successfully log to the console on page load, but won't populate the array in my ng-repeat until I click around on the page:

    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加载页面如下:

My controller is loaded with the page via app.js as follows:

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

这是与SharePoint的JSOM需要一些正确执行,如何我想在AngularJS,或别的东西来执行的问题吗?

Is this an issue with SharePoint's JSOM needing something to execute properly, how I am trying to execute in AngularJS, or something else?

推荐答案

我能得到这个工作。问题是,角度似乎并不尊重无棱角的功能,因此使用$ scope.apply在我的数组$范围的推动是有效的。需要注意的是环绕整个功能引起的问题与角的摘要:

I was able to get this working. The problem was that Angular doesn't appear to respect non-Angular functions, so using $scope.apply around the push of my array to $scope was effective. Note that wrapping the entire function caused issues with the digest of 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());
            });
          };

这篇关于执行功能的SharePoint页面加载在AngularJS应用($范围的问题???)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆