Is there a way to add operators to AngularJS expression? - angularjs

I would like to add beginsWith (^=) and endsWith ($=).
E.g., scope.$eval('"abcd" ^= "a"') should return 'true'

According to Angular's documentation on expressions, you should use controller\filter:
No function declarations or RegExp creation with literal notation
You can't declare functions or create regular expressions from within
AngularJS expressions. This is to avoid complex model transformation
logic inside templates. Such logic is better placed in a controller or
in a dedicated filter where it can be tested properly.

Related

filtering complex collections by keywords

Is any way to filter a complex collection by keyword? We have a complex entity named Phase, and Phase has a property named EntityTitle, we want to filter out all the Phase whose title contains the keyword "Completed". I tried both contains and search.ismatch, but none of them support. Appreciate any idea. Thanks.
"Filter":"(Phases/any(phase: contains(phase/EntityTitle, 'Completed')))"
"Filter":"(Phases/any(phase: search.ismatch('Completed', 'phase/EntityTitle')))"
Indeed "search.ismatch" and "contains" are not supported inside lambda expressions when filtering complex types, you should instead use "eq". See https://learn.microsoft.com/en-us/azure/search/search-query-troubleshoot-collection-filters#cheat-sheet-for-collection-filter-rules
An example for your scenario would be
Phases/any(phase: phase/EntityTitle eq 'Completed')

Xtend: evaluate expression directly from a string

Is there a possibilty in the Xtend language to evaluate an expression directly from a string, e.g. like Eval in Groovy. I want to do something like this in Xtend (the example is from Groovy):
import groovy.util.Eval
assert Eval.me('2*5') == 10
If there is no built-in way to do this, what would be the most similar alternative to achieve this (if any)?
P.S. Just to be clear: the expression is of course not just a simple math operation (like in the example); in particular, I would like to call my own Xtend function doing some transformation on a list.
I think there isn't anything like this in Xtend, so you should probably look for Java libraries that do this.
For example Java EL seems like a good standard way for evaluating strings. Since EL 3 there is the ELProcessor which doesn't require JSP anymore and it seems quite easy to use:
ELProcessor elp = new ELProcessor();
elp.defineBean("employee", new Employee("Charlie Brown"));
String name = elp.eval("employee.name");
Here is nice article about the latest features of EL, like lambda expression. The article also contains some examples about collections and how to call external methods.

AngularJS: Directive with arbitrary start and end symbols

What?
I'd like to use an Angular .directive by using arbitrary start and end symbols, e.g. square brackets:
<myDirective>callDirectiveAsUsual</myDirective><!-- usual directive call -->
<div>[[callDirectiveWithArbitrarySymbols]]</div><!-- new directive call -->
Why?
To make using a translation library much easier. Instead of annotating HTML, I would like to write [[TranslateMePlease!]].
Questions
Is it possible to define custom start and end symbols for a .directive? (According to the docs, this is not possible.)
Do I have to add a custom $interpolateProvider to the interpolation pipeline? (Assuming there is such an an interpolation pipeline in Angular.)
Do I have to write my own version of ng-bind? ng-bind-custom which uses its own $interpolateProviderCustom, which looks out for square brackets?

Can i use or logic in ng-when

I would like to do something like this:
<ANY ng-switch-when="matchValue1 || matchValue2">...</ANY>
To simplify my expression, currently I do that same thing for matchValue1 and matchValue2.
According to angular web site "Be aware that the attribute values to match against cannot be expressions. They are interpreted as literal string values to match against. For example, ng-switch-when="someVal" will match against the string "someVal" not against the value of the expression $scope.someVal."
Using ng-show would be useful in your case.

extjs function declaration syntax

In extjs, we often have syntax like this:
someFunction = function(){}
or:
someFunction : function(){}
What is the difference between the two? Also, what enables exts to use this syntax as opposed to the normal javascript syntax?
So far as i know, javascript syntax is like this:
function(){}//No '=' or ':'
There is not ExtJS function syntax. All these methods of defining a function are part of JavaScript and there is nothing new introduced by ExtJS. Lets take each case
function functionname() -
This is most common and is coming from the procedural programming school. Basically you are writing global functions and these are called by other functions in your script
Enter OOP in Javascript.. there is where the next two methods come in! Javascript is very flexible and extensible. Functions can be stored in variables, passed into other
functions as arguments, passed out of functions as return values, and constructed at run-time. You can also have anonymous functions! coming back...
someFunction = function() - In this case, you are storing a function in the variable 'comeFunction'.This variable can be part of an object or separate (But internally everything in javascript is object except for primitive data types).
someFunction : function() - In this case also, you are storing the function in the variable but this is during object declaration. You will see them used in ExtJS because it follows OOP.
You could also inject a method or modify the method you already specified by the above two methods. I hope this helps you understand more about functions.

Resources