angularjs - How to bind variables to INPUT dynamically -
i have object in controller value getting http request:
$scope.myobj = { id1: "1", id2: "5", id3: "", id4: "" }; it can have number of fields (id...) values. if id-k not empty id-n n < k not empty too.
i need bind input last not empty field of object. best way it?
here plnkr
update: object position in classificator. in example id of position 1.5. need allow edit last position in classification. user can change 5 6, 7 or else, can not change 1 segment
if object be
$scope.myobj = { id1: "2", id2: "5", id3: "4", id4: "8" }; the classification 2.5.4.8 , user must able edit last segment - 8.
controller
... $scope.myobj = { id1: "1", id2: "5", id3: "6", id4: "" }; $scope.segments = object.keys($scope.myobj) .filter(function(key) { return $scope.myobj[key]; }) .sort(function(a, b){ return number(a.replace('id', '')) - number(b.replace('id', '')); }); ... html
<span ng-repeat="segment in segments"> <span ng-if="!$last">{{ myobj[segment] }}</span> <input ng-if="$last" ng-model="myobj[segment]"> </span> <p>{{ myobj }}</p> plnkr - https://plnkr.co/edit/8c9llldcvvzfinaoxqtd?p=preview
original answer
you can calculate last non blank id in controller , set scope variable. like
$scope.mylast = $scope.myobj['id' + object.keys($scope.myobj).reduce(function(a, key){ if ($scope.myobj[key]) { var idnum = number(key.replace('id', '')); = math.max(a, idnum) } return a; }, -infinity)]; note - can't rely on order of keys, can't assume last key biggest one.
plnkr - https://plnkr.co/edit/apy8qznjnk4q23j5ja4n?p=preview
Comments
Post a Comment