2016-09-30 6 views
0

hat Ich versuche, eine Direktive zu erstellen, die ein Argument hat, das ein Objekt ist. Dieses Objekt hat eine Eigenschaft map, die eine Funktion sein kann.eckige direktive map Objekt, das Funktion innerhalb

Jedes Mal, wenn ich es laufen bekomme ich diese Fehler

https://docs.angularjs.org/error/ $ Parse/Syntax? P0 =% 7B & p1 = ist% 20unexpected,% 20expecting% 20% 5B% 7D% 5D & p2 = 159 & p3 =% 5B% 7B% 20name% 20:% 20% 27Nom% 27,% 20map% 20:% 20% 27id% 27% 20% 7D,% 7B% 20Name% 20:% 20% 27Beschreibung% 27,% 20map% 20:% 20% 27titre_fr% 27% 20% 7D,% 7B% 20name% 20:% 20% 27Typ% 20de% 20Valeur% 27,% 20map% 20:% 20% 27type_valeur% 27% 20% 7D,% 20% 7B% 20name% 20:% 20% 27test% 27,% 20map% 20:% 20funktion (d)% 20% 7B% 20zurück% 20% 27ok% 27;% 20% 7D% 7D% 5D & p4 =% 7B% 20zurück% 20% 27ok% 27;% 20% 7D% 7D% 5D

Meine Richtlinie wird wie folgt definiert:

directive.js

.directive('apiTable', ['$rootScope', 'APIService', function($rootScope, APIService) 
{ 
    return { 
     restrict : 'E', 
     templateUrl : '/js/angular/app/ui/api-table.html', 
     transclude : true, 
     scope : 
     { 
      mapping : '=', 
      route : '@' 
     }, 
     controller : function($scope) 
     { 
      $scope.data = []; 
      $scope.page = 0; 
      this.mapResult = function(data) 
      { 
       for(var i = 0; i < data.length; i++) 
       { 
        var temp = []; 
        for(var j = 0; j < $scope.mapping.length; j++) 
        { 
         temp[0] = i + 1; 
         if(typeof(data[i][$scope.mapping[j].map]) !== 'undefined') 
         { 
          if(typeof(data[i][$scope.mapping[j].map]) == 'function') 
          { 
           console.log('ok') 
          } 
          else 
          { 
           temp[j + 1] = data[i][$scope.mapping[j].map]; 
          } 
         } 
         else 
         { 
          temp[j + 1] = ""; 
         } 
        } 
        $scope.data.push(temp); 
       } 
      }; 
      this.getData = function(p) 
      { 
       APIService.getData($scope.route, p) 
        .then(function(data) 
        { 
         this.mapResult(data.results); 
        }.bind(this)); 
      }; 
      this.getData({ p : $scope.page }); 
     } 
    }; 
}]) 

und der Anruf wird wie folgt aus:

view.html

<api-table route="_dictionnaryAPIFindByDomain" mapping="[{ name : 'Nom', map : 'id' },{ name : 'Description', map : 'titre_fr' },{ name : 'Type de Valeur', map : 'type_valeur' }, { name : 'test', map : function(d) { return 'ok'; }}]"></api-table> 

Antwort

1

Keine Funktionserklärungen: Sie können Funktionen in einem Angular-Ausdruck nicht deklarieren, nicht einmal innerhalb der ng-init-Direktive.
Angular Expressions

Es ist wie diese Regel sieht gilt auch Objekteigenschaften

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app> 
 
    {{ {a:'b',c:function(){}} }} 
 
</div>

einfachste Lösung ist es zu JSON zu kodieren und dann analysieren.

+0

Das ist nicht so einfach. Meine Zuordnung erfolgt in der HTML-Datei. Um es mit json zu stringieren, muss ich JS verwenden. – T00rk

+0

Könnten Sie in Ihrer Frage schreiben, wie das Mapping gemacht/gesetzt wird? Es ist seltsam, Funktionen in Ihrem HTML zu konstruieren. –

Verwandte Themen