在AngularJS应用程序页面加载时执行SharePoint函数($scope issue??)
Execute SharePoint Function On Page Load in AngularJS Application ($scope issue???)
我在一个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());
});
};
相关文章:
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 将函数的上下文应用于javascript变量
- 如何在JavaScript中将字符串转换为函数引用
- 用嵌套函数和默认函数定义函数
- 使用 jQuery 的 .on 函数如何获取事件的原始元素
- 无法导出函数expressjs/requestjs中的变量
- 函数参数中的数据与指定变量之间的任何性能差异
- JQuery合并了keyup和focusout两个函数
- ES6构造函数返回基类的实例
- 监视函数从服务返回不起作用,但作用域函数起作用
- 我可以在json对象中添加一个函数吗
- AngularJS:我可以跳过函数参数回调吗
- 如何使jQuery插件函数可调用以供独立使用,而不在集合上操作
- JavaScript数组排序(函数)用于对表行进行排序,而不是排序
- jquery点击函数select&取消选择
- 拨打'父亲'函数形式a'儿童'ReactJS中的组件
- Node.js v6.2.0类扩展不是函数错误
- 将参数传递给回调函数:Scope issue
- jquery ajax issue..done() 函数中的奇怪行为
- 在AngularJS应用程序页面加载时执行SharePoint函数($scope issue??)