Rohit Ghotkar Rohit Ghotkar - 1 month ago 11
AngularJS Question

append id to scope variable in angular js

I am building app where i want to dynamically create one array which i have to assign to dynamically created table using ng-repeat.
in my directive i have append tr like :

link: function (scope, elem, attrs) {
$(elem).click(function () {
var tmpl="<tr ng-repeat='orders in allCurrentTakeAwayOrder'>
</tr>";
});
}


and this is repeating for multiple time on button click event.
I want to append a dynamic id to this as

<tr ng-repeat='orders in allCurrentTakeAwayOrder"+scope.divId+"'>


and it is appending successfully,
But problem is thathow can i append same id to that varible in controller, when assigning a data to that variable ?

app.controller('orderController', function ($scope){

$scope.allCurrentTakeAwayOrder **<i want to append that id here>** ="data will be here to be display in table"

});

Answer

To append scope.divId to scope variable name, use a function with a property accessor:

<!-- replace with function
   <tr ng-repeat='orders in allCurrentTakeAwayOrder"+scope.divId+"'>
-->

<tr ng-repeat='orders in computedList()'>

JS

scope.computedList = function() {
    return scope["allCurrentTakeAwayOrder"+scope.divId];
};
Comments