Użyj podkreślenia wewnątrz kontrolerów kątowych
Jak używać biblioteki podkreślenia wewnątrz kontrolerów angularjs?
On this post: AngularJS limitTo by last 2 records ktoś zasugerował przypisanie zmiennej _ do rootScope, aby Biblioteka była dostępna dla wszystkich zakresów w aplikacji.
Ale nie wiem, gdzie to zrobić. Chodzi mi o to czy powinno iść na deklarację modułu app? i. E:var myapp = angular.module('offersApp', [])
.config(['$rootScope', function($rootScope) { }
Ale gdzie wczytać underscore lib? Po prostu mam na mojej stronie indeksu ng-app dyrektywa i skrypt odwołują się do bibliotek angular-js i underscore?
index.html
:
<head>
</head>
<body ng-app="offersApp">
...
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="scripts/vendor/angular.js"></script>
<script src="scripts/vendor/underscore.js"></script>
...
Jak to osiągnąć? 6 answers
Kiedy dodasz podkreślenie, przywiązuje się ono do obiektu window
, a więc jest dostępne globalnie.
Więc możesz użyć go z kodu kątowego tak, jak jest.
Możesz go również zapakować w Serwis lub fabrykę, jeśli chcesz, aby został wstrzyknięty:
var underscore = angular.module('underscore', []);
underscore.factory('_', ['$window', function($window) {
return $window._; // assumes underscore has already been loaded on the page
}]);
A następnie możesz poprosić o _
w module aplikacji:
// Declare it as a dependency of your module
var app = angular.module('app', ['underscore']);
// And then inject it where you need it
app.controller('Ctrl', function($scope, _) {
// do stuff
});
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-02 15:39:41
Zaimplementowałem tutaj sugestię @ satchmorun: https://github.com/andresesfm/angular-underscore-module
Aby go użyć:
-
Upewnij się, że dodałeś podkreślenie.js w Twoim projekcie
<script src="bower_components/underscore/underscore.js">
-
Pobierz:
bower install angular-underscore-module
-
Dodaj angular-underscore-module.js do pliku głównego (indeks.html)
<script src="bower_components/angular-underscore-module/angular-underscore-module.js"></script>
-
Dodaj moduł jako zależność w definicji aplikacji
var myapp = angular.module('MyApp', ['underscore'])
-
Aby użyć, Dodaj jako zaimplementowana zależność do kontrolera / usługi i jest gotowa do użycia
angular.module('MyApp').controller('MyCtrl', function ($scope, _) { ... //Use underscore _.each(...); ...
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-06-05 15:07:07
Używam tego:
var myapp = angular.module('myApp', [])
// allow DI for use in controllers, unit tests
.constant('_', window._)
// use in views, ng-repeat="x in _.range(3)"
.run(function ($rootScope) {
$rootScope._ = window._;
});
Zobacz https://github.com/angular/angular.js/wiki/Understanding-Dependency-Injection mniej więcej w połowie, aby uzyskać więcej informacji na temat run
.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-06-01 21:29:27
Możesz również spojrzeć na ten moduł dla angular
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-16 04:52:57
Jeśli nie masz nic przeciwko użyciu lodash wypróbuj https://github.com/rockabox/ng-lodash lodash lodash jest więc jedyną zależnością i nie trzeba ładować żadnych innych plików skryptowych, takich jak lodash...........:):):):):):):):):):):):):):):):)
Lodash jest całkowicie wyłączony z zakresu okna i nie ma "nadziei", że został załadowany przed Twoim modułem.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-07-02 19:45:43
Możesz użyć tego modułu - > https://github.com/jiahut/ng.lodash
To jest dla {[0] } tak jak underscore
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-18 04:24:11