Wednesday 15 April 2015

angularjs - angular sortable with model data from json -



angularjs - angular sortable with model data from json -

i wanna utilize angular sortable on app. model dynamically populated several json files $http.get() function. ngsortable see model empty array. , won't new info json file. there workaround this?

class="snippet-code-js lang-js prettyprint-override">$scope.jsons = ["data1.json", "data2.json"]; $scope.abc = []; angular.foreach($scope.jsons, function(value, key){ $http.get(value).success (function(data){ $scope.abc.push(data); }); }); $scope.sortableoptions = { accept: function (sourceitemhandlescope, destsortablescope) {return true} }; class="snippet-code-html lang-html prettyprint-override"><div ng-model="abc" as-sortable="sortableoptions"> <div ng-repeat="x in abc" as-sortable-item> <div as-sortable-item-handle>{{x.name}}</div> </div> </div>

i had same problem ng-sortable: worked fine static info not json info come asynchronous $http.get().

there 2 solutions:

leave controller and, in html part, replace both occurrences of "abc" "$parent.abc"

instead of straight access 'abc' array, utilize intermediate object, this:

$scope.tmpobject = {}; $scope.tmpobject.abc=[]; ... $http.get(value).success (function(data){ $scope.tmpobject.abc.push(data); });

...

<div ng-model="tmpobject.abc" as-sortable="sortableoptions"> <div ng-repeat="x in tmpobject.abc" as-sortable-item> <div as-sortable-item-handle>{{x.name}}</div> </div> </div>

json angularjs

No comments:

Post a Comment