2016-09-30 111 views
0

我尝试创建一个具有参数的指令,该指令是一个对象。 该物品有一个属性map,可以是一个功能。内部具有函数的角度指令映射对象

每次我运行它,我得到这个错误

https://docs.angularjs.org/error/ $解析/语法?P0 =%7B & P1 =为%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%27描述%27% 20map%20:%20%27titre_fr%27%20%7D,%7B%20name%20:%20%27Type%20de%20Valeur%27%20map%20:%20%27type_valeur%27%20%7D,% 20%7B%20%20%20%27%27%20%20%%20%(d)%20%7B%20%20%27%27%20%7D%7D%5D & p4 = 7B%20返回%20%27ok%27;%20%7D%7D%5D

我的指令定义是这样的:

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 }); 
     } 
    }; 
}]) 

和通话是这样的:

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> 

回答

1

无函数声明:即使在ng-init指令中,也不能在Angular表达式中声明函数。
Angular Expressions

它看起来像这条规则也适用于对象属性

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

最简单的解决是将其编码为JSON,然后解析它。

+0

这并不是那么简单。我的映射是在html文件中进行的。要使用json进行字符串化,我必须使用JS。 – T00rk

+0

你可以在你的问题中发布如何进行映射吗?在你的html中构造函数似乎很奇怪。 –