1

Javascript Angular has a peculiar pattern for creating controllers that looks like this:

myApp.controller('myController', ['$scope', 'foo', 'bar', 
                 function($scope, foo, bar) {
                     //do stuff...
                 }]);

The '$scope', 'foo', 'bar' thing is repeated as the parameter list in the function inside the square-bracketed array.

I thought I could make doing this less error-prone by writing a yasnippet. It looks like this:

# -*- mode: snippet -*-
# name: angctl
# key: angctl
# --
$1.controller('$2', ['$3', function($3) {
$0;
}]);

This works fine, but only if there is one parameter (in the $3 position), like if I'm just using $scope. If I want to use more than one, it breaks down.

Is there some way of writing the snippet so it will make a comma delimited list of quoted strings and repeat a comma delimited list of unquoted strings somewhere else within the snippet?

3

I guess something like this might work:

# -*- mode: snippet -*-
# name: angctl
# key: angctl
# --
$1.controller('$2', [${3:$(mapconcat (lambda (x) (format "'%s'" x))
                                     (split-string yas-text "," t "[ \t]") ", ")}, function($3) {
$0;
}])

And in action:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.