Can I have JavaScript functions inside of AngularJS curly braces/notation?
I have tried the code below. If you un-comment my JS functions they don't work.
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="">
<p>Some formula: {{ 1+2 }}</p>
<!--
<p>Some function: {{Math.sin(12)}}</p>
<p>Some constructor: {{new Date()}}</p>
-->
</div>
Is it possible to unlock/enable?
UPDATE
Okay, in simple {{}} functions can be wrapped into UDFs. But what about functions inside cell template in UI Grid? I still can use simple formulas there, but can't use any functions, even UDFs defined in $scope.
In the sample below, cellTemplateWorks uses simple addition and it works. The template cellTemplateNotWorks uses call to myFunction and not works in template (while works in simple placeholder).
var myApp = angular.module('myApp', ['ui.grid', 'myControllers']);
var myControllers = angular.module('myControllers', []);
myControllers
.controller('MyController', ['$scope', function ($scope) {
$scope.myData = [{"x": 1}, {"x": 2}, {"x": 3}, {"x": 4}, {"x": 5}];
var cellTemplateWorks = "<div \
class=\"ngCellText\" ng-class=\"col.colIndex()\"><span ng-cell-text>\
{{row.entity[col.field]+1}}\
</span></div>";
var cellTemplateNotWorks = "<div \
class=\"ngCellText\" ng-class=\"col.colIndex()\"><span ng-cell-text>\
{{myFunction(row.entity[col.field])}}\
</span></div>";
$scope.gridOptions = {
data: 'myData',
columnDefs: [
// {field: 'x', displayName: 'x', },
// {field: 'x', displayName: 'sin(x)', cellTemplate: cellTemplateNotWorks},
{field: 'x', displayName: 'sin(x)', cellTemplate: cellTemplateWorks},
]
};
$scope.myFunction = function(arg) {
return Math.sin(arg);
};
}]);
.myGrid {
width: 500px;
height: 250px;
}
<link rel="styleSheet" href="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-grid/3.0.7/ui-grid.css"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-grid/3.0.7/ui-grid.js"></script>
<body ng-app="myApp">
<div ng-controller="MyController">
<p>MyFunction does work: {{myFunction(12)}}</p>
<p>MyFunction does not work in UI Grid:
<div ui-grid="gridOptions" class="myGrid"></div>
</p>
</div>
</body>