Angular Underscore
underscore + isolated scope
by RopoMen
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.3/angular.js"></script>
<div ng-app="myApp">
<div ng-controller="myCtrl">
<h3>Normal $scope</h3>
<span ng-if="is_it">"$scope.isEmpty" is a function!</span>
<span ng-if="!is_it">"$scope.isEmpty" is <b>not</b> a function!</span>
<br />
<h3>Isolated $scope</h3>
<div my-solated="bar"></div>
<br />
<p>If you set 'my-solated' directive "scope: true", "isEmpty" is a function, but if you use "{}" notation it is not a function anymore.</p>
</div>
</div>
JavaScript
angular.module('myApp', ['angular-underscore'])
.config([function() {
}])
.directive('mySolated', [function() {
return {
restrict: 'A',
scope: {
foo: '@mySolated'
},
template: '<span ng-if="is_it_still">"$scope.isEmpty" is a function!</span><span ng-if="!is_it_still">"$scope.isEmpty" is <b>not</b> a function!</span><br /> <span ng-bind="foo"></span>',
link: function(scope) {
scope.is_it_still = angular.isFunction(scope.isEmpty);
}
};
}])
.controller('myCtrl', ['$scope', function($scope) {
$scope.is_it = angular.isFunction($scope.isEmpty);
}]);
(function (ng, _) {
'use strict';
var
underscoreModule = ng.module('angular-underscore', []),
utilsModule = ng.module('angular-underscore/utils', []),
filtersModule = ng.module('angular-underscore/filters', []);
// begin custom _
function propGetterFactory(prop) {
return function(obj) {return obj[prop];};
}
_._ = _;
// Shiv "min", "max" ,"sortedIndex" to accept property predicate.
_.each(['min', 'max', 'sortedIndex'], function(fnName) {
_[fnName] = _.wrap(_[fnName], function(fn) {
var args = _.toArray(arguments).slice(1);
if(_.isString(args[2])) {
// for "sortedIndex", transmuting str to property getter
args[2] = propGetterFactory(args[2]);
}
else if(_.isString(args[1])) {
// for "min" or "max", transmuting str to property getter
args[1] = propGetterFactory(args[1]);
}
return fn.apply(_, args);
});
});
// Shiv "filter", "reject" to angular's built-in,
// and reserve underscore's feature(works on obj).
ng.injector(['ng']).invoke(['$filter', function($filter) {
_.filter = _.select = _.wrap($filter('filter'), function(filter, obj, exp, comparator) {
if(!(_.isArray(obj))) {
obj = _.toArray(obj);
}
return filter(obj, exp, comparator);
});
_.reject = function(obj, exp) {
// use...