我在一个JavaScript应用程序中使用SharePoint的AngularJS对象模型,并且需要它的一个函数来执行页面加载,所以一个数组被填充并被ng重复使用。
当前,它会在页面加载时将数组推送到控制台,但似乎不会将这些值提交到数组中,以便在页/$范围上使用,直到我单击in/然后从输入框中,单击警报框上的OK,或使我保持在页面上的虚拟链接。这就是为什么它可以在加载时正确地记录数组,但没有在$scope中为页面准备好数组的原因。
我试过以下几种方法,但都没有成功:
1)我已经将执行函数的调用移到控制器的底部
2)我尝试过angular.element(document).ready
3)我尝试过将它包装成这样的函数:
$scope.initTaxonomy = function () {
$(function () {
// Function here
});
};我不明白的是,为什么调用REST服务的以下内容在页面加载/工作时执行得很好,将其推入$scope以便在ng重复中使用,而我的函数却没有:
// 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-重复中的数组:
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中执行的,还是其他的?
发布于 2015-05-21 13:52:52
我能做到这点。问题是角似乎不尊重非角函数,所以使用$scope.apply将我的数组推到$scope上是有效的。请注意,包装整个函数会导致角摘要出现问题:
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());
});
};https://stackoverflow.com/questions/30349856
复制相似问题