I just started learning AngularJS two days ago, so if you think I did not approach this in an Angular sort of way then please guide me. So the code below is part of my HTML which has the ng-repeat directive
<div class="row" data-ng-repeat="button in buttons" ng-if="$index % 3 == 0">
<div class="col-lg-3 col-lg-offset-1">
<button class="btn btn-default button-width" data-ng-click="my_func('{{buttons[$index].desc}}')">{{buttons[$index].name}}</button>
</div>
<div class="col-lg-3 col-lg-offset-1">
<button class="btn btn-default button-width" data-ng-click="my_func('{{buttons[$index + 1].desc}}')">{{buttons[$index + 1].name}}</button>
</div>
<div class="col-lg-3 col-lg-offset-1">
<button class="btn btn-default button-width" data-ng-click="my_func('{{buttons[$index + 2].desc}}')">{{buttons[$index + 2].name}}</button>
</div>
</div>
The following code is my Angular Controller which is passing the values to the ng-repeat directive in the HTML and then the ng-click directive in the HTML is calling the my_func function in the controller.
remote_app.controller("remote-controller", function($scope, $http) {
$scope.buttons = [{
"desc": "OFF_V1",
"name": "OFF",
}, {
"desc": " ",
"name": " ",
}, {
"desc": "SOURCE_1",
"name": "SOURCE",
}];
$scope.my_func = function(command) {
$http.get("http://localhost:1337?comm=" + command).success(function() {
$scope.result = "Sent to server - " + command;
});
};
})
Now this displays the values fine from the array when the HTML page is rendered but when i click the button which calls the ng-click directive and in turn my_func, the value sent over to the server is not the rendered value of my_func('OFF_V1') but instead the value sent is my_func('{{buttons[$index].desc}}'). I have tried to find a solution but right now things like $apply and $digest are beyond my understanding of Angular , so any help is appreciated.
{{ }}in your function calls either, that is for string interpolation. Meaning, you will use{{ }}inside your html in order to write that value to the view. - Tj Giengerfunction($index)which is nice for array manipulation. - Tj Gienger$indexinto the function and access that specific button from the array inside the controller rather than all that stuff in the html. - Tj Gienger