Question

I have a problem with my AngularJS Application, I try to call a function in my Controller but the parameter when I console.log(id); , id is an array...

I Use [ ] instead of {{ }} because i'm working with TWIG

Angular Template (html source)

<div class="ext-[f.ext]" ng-repeat="f in files">
    <input type="checkbox" ng-click="select([f.id],'file')">
    <div>[f.name]</div>
</div>

Rendered by Angular

<div class="ext-html" ng-repeat="f in files">
    <input type="checkbox" ng-click="select(27,'file')">
    <div ng-binding">index.html</div>
</div>

My JS function

$scope.select = function (id ,type) {
    console.log(id); // Array
    console.log(type); // String (as expected)
}

console Output :

[28] app.js:74
file app.js:75

The problem is that I have this which works...

<div class="folder row" ng-repeat="f in folders" ng-click="getFolder([f.id])">
    <div class="col-md-12">[f.name]</div>
</div>
Was it helpful?

Solution

You're specifying an array as the parameter.

<input type="checkbox" ng-click="select([f.id],'file')">

Should be

<input type="checkbox" ng-click="select(f.id,'file')">

You create arrays in javascript using the square brackets.

var array = [ 1, 2, 3, 4];

So you created an array with a single value in which was your id.

  1. Even if you use the same syntax as an array for your bindings you don't need to use bindings in that context.
  2. You shouldn't "override" a standard language construct to do something completely different as you might get unexpected behaviour
  3. Proof http://plnkr.co/edit/4n2yg6QudzcFA5bazjZ3?p=preview
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top