javascript - Angularjs in sharepoint 365 -
i trying set variable callback tried $apply got error $apply conflicting, after research found out should use $timeout instead of $apply. changed code shown below.
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 = {}; }]);
i trying achieve in function setjsondata, can see alert still $scope.stdformtemplate not getting updated expected.
i trying use this library.
i recommend using $q , promises. can externalize data access in factory/service , use promises handle async data access operations.
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 = []; // 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 untested, , written on-the-fly, minor adjustments/fixes may neccesary.
Comments
Post a Comment