Javascript sharepoint 365中的Angularjs
我试图从回调中设置一个变量,我尝试了$apply,但我得到了一个错误$apply是冲突的,经过一些研究,我发现我应该使用$timeout而不是$apply。因此,我更改了代码,如下所示Javascript sharepoint 365中的Angularjs,javascript,angularjs,sharepoint,sharepoint-online,Javascript,Angularjs,Sharepoint,Sharepoint Online,我试图从回调中设置一个变量,我尝试了$apply,但我得到了一个错误$apply是冲突的,经过一些研究,我发现我应该使用$timeout而不是$apply。因此,我更改了代码,如下所示 myApp.controller('dynamicCtrl', ['$scope', '$timeout', function ($scope, $timeout) { var jsonObj = []; jsonObj.push({ "type": "text",
myApp.controller('dynamicCtrl', ['$scope', '$timeout', function ($scope, $timeout) {
var jsonObj = [];
jsonObj.push({
"type": "text",
"model": "text",
"label": "text",
"placeholder": "text"
});
//$scope.stdFormTemplate = jsonObj;
var ctx = new SP.ClientContext.get_current();
var web = ctx.get_web();
var list = web.get_lists().getByTitle('TITLE');
var listFields = list.get_fields();
clientContext.load(listFields);
ctx.load(listFields);
ctx.executeQueryAsync(function () {
onListFieldsQuerySucceeded(setJsonData);
},
function (sender, args) {
console.log(args);
});
function onListFieldsQuerySucceeded(callback) {
var type = "";
var fieldEnumerator = listFields.getEnumerator();
while (fieldEnumerator.moveNext()) {
var oField = fieldEnumerator.get_current();
var fType = oField.get_fieldTypeKind();
if (fType === SP.FieldType.choice) {
} else if (fType === SP.FieldType.text) {
}
}
callback(jsonObj);
}
function setJsonData(jsonObj) {
$timeout(function () {
alert("hithere" +jsonObj);
$scope.stdFormTemplate = jsonObj;
}, 0);
}
$scope.stdFormData = {};
$scope.urlFormData = {};
}]);
我试图在函数setJsonData中实现这一点,我可以看到警报,但$scope.stdFormTemplate仍没有按预期更新
我正在尝试使用library。我建议使用$q和promises。 您可以在工厂/服务中外部化数据访问,并使用承诺来处理任何异步数据访问操作
myApp.service('spAccess', function($q) {
this.getListData = function (listName) {
var deferred = $q.defer();
var ctx = SP.ClientContext.get_current();
var list = ctx.get_web().get_listByTitle(listName);
ctx.load(list);
ctx.executeQueryAsync(function() {
var items = [];
// Get your data here
deferred.resolve(items);
}, function(err, args){
deferred.reject([err, args});
});
return deferred.promise;
};
});
myApp.controller('ctrl', function(spAccess, $scope){
spAccess.getListData('MY LIST').then(function(result){
$scope.items = result;
}, function(err){
console.log(err);
});
});
// NOTE: Code was untested, and written on-the-fly, so some minor adjustments/fixes may be neccesary.
看看Angular或jQuery中的延迟对象