我尝试创建一个具有参数的指令,该参数是一个对象。
该对象具有可以作为函数的属性map
。
每次我运行它都会出现此错误
https://docs.angularjs.org/error/ $解析/语法P0 =%7B&安培; P 1 =是%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%27Description%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%20name%20:%20%27test %27%20map%20:%20function(d)%20%7B%20return%20%27ok%27;%20%7D%7D%5D&安培; P4 =%7B%20return%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>
答案 0 :(得分: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,然后解析它。