我在Angular控制器中使用SharePoint JavaScript对象模型从分类法(术语库)中检索数据。通过使用$ scope.apply,我能够将数组绑定到范围并使用下拉列表中的值,因为SharePoint的JavaScript对象模型不是范围所理解的正常Angular函数。这可以按预期工作。
现在我需要将字段的值设置为存储在数据库中的当前值。这适用于下拉列表/基于选择的字段,我通过搜索数组来检索项目的索引。例如:
var currentCategoryIndex = $scope.categoryValues.map(function (e) { return e.value; }).indexOf(currentCategoryValue);
$scope.vm.selectedCategory = $scope.categoryValues[currentCategoryIndex];
但是,我无法访问控制器中的数组以检查索引(请参阅下面的代码)。但是,它确实通过$ scope。$ apply绑定$ scope用于下拉列表。
其他一些奇怪的事情是,如果我添加一个警报,它将开始工作,就像它以某种方式迫使范围回来。但是每次只是为了让阵列工作而在页面加载时使用警报是不现实的。
我需要访问数组,以便我可以与它进行比较并获取索引,以便我可以将字段值设置为当前存储在数据库中的正确项目。
这是我控制器中的功能。请注意,我需要运行一个子函数来获取所有值。这可以创建我在下拉列表中使用的$ scope.termsArray绑定,它是发生问题的$ scope.vm.selectedCategory的设置:
var termsArray = [];
// Query Term Store and get terms for use in Managed Metadata picker stored in an array named "termsArray".
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. Note, that if you receive the following error "Specified argument was out of the range of valid values. Parameter name: index", you may need to check the term store name under Term Store Management to ensure it was not changed by Microsoft
var termStore = termStores.getByName("Taxonomy1234");
// GUID of Term Set from which to get the Terms
var termSet = termStore.getTermSet("1234");
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();
var termLabel = currentTerm.get_name();
// Get labels (synonyms) for each term and push values to array
getLabels(guid, guidString, termLabel);
}
// Set $scope to terms array
$scope.$apply(function () {
$scope.termsArray = termsArray;
console.log($scope.termsArray); // DOES NOT LOG ARRAY
});
var currentFacilityIndex = termsArray.map(function (e) { return e.termGUID; }).indexOf(currentFacilityGUID);
console.log(currentFacilityIndex);
$scope.term.selected = termsArray[currentFacilityIndex];
}, function (sender, args) {
console.log(args.get_message());
});
// Get labels (synonyms) for each term and push values to array
function getLabels(termguid, guidString, termLabel) {
var clientContext = SP.ClientContext.get_current();
var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(clientContext);
var termStores = taxSession.get_termStores();
// The name of the term store. Note, that if you receive the following error "Specified argument was out of the range of valid values. Parameter name: index", you may need to check the term store name under Term Store Management to ensure it was not changed by Microsoft
var termStore = termStores.getByName("Taxonomy1234");
// GUID of Term Set from which to get the Terms
var termSet = termStore.getTermSet("1234");
var term = termSet.getTerm(termguid);
var labelColl = term.getAllLabels(1033);
clientContext.load(labelColl);
clientContext.executeQueryAsync(function () {
var labelEnumerator = labelColl.getEnumerator();
var synonyms = "";
while (labelEnumerator.moveNext()) {
var label = labelEnumerator.get_current();
var value = label.get_value();
synonyms += value + " | ";
}
termsArray.push({
termName: termLabel,
termGUID: guidString,
termSynonyms: synonyms
});
}, function (sender, args) {
console.log(args.get_message());
});
}
};
// Execute function
execOperation();
更新:我尝试设置$ scope.termsArray = [];根据下面的建议,但它没有奏效。真正奇怪的是,如果我有如下警告,它会以某种方式迫使控制台记录/授予我对控制器中阵列的访问权。
$scope.$apply(function () {
$scope.termsArray = termsArray;
alert("hey");
console.log($scope.termsArray);
});
答案 0 :(得分:2)
我发现有点难以遵循您的代码。
我的第一个猜测是在其他任何东西之前用空值实例化数组。
$scope.termsArray = [];
这个技巧告诉Angular这个属性存在并且将在以后阶段存在。