Last active
April 19, 2024 15:59
-
-
Save yue1123/b0af4333ef9f03bc307d7dfcb7576805 to your computer and use it in GitHub Desktop.
Angular.js any order function params Inject
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const instance = { | |
| $scope: '我是$scope', | |
| $state: '我是$state', | |
| $timeout: '我是$timeout', | |
| $uibModal: '我是$uibModal' | |
| } | |
| let FN_ARGS = /^[^\(]*\(\s*([^\)]*)\)/m | |
| let FN_ARG_SPLIT = /,/ | |
| let FN_ARG = /^\s*(_?)(\S+?)\1\s*$/ | |
| function createModule(moduleName, entry) { | |
| console.log(moduleName) | |
| const fnText = entry.toString() | |
| const args = fnText.match(FN_ARGS)[1].replace(/[\s\r\n]+/, ' ') | |
| const $inject = [] | |
| args.split(FN_ARG_SPLIT).forEach((item) => { | |
| item.replace(FN_ARG, (_, __, c) => { | |
| $inject.push(instance[c]) | |
| }) | |
| }) | |
| entry.apply(entry, $inject) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const testModule = createModule('testModule', function ($uibModal, $state, $scope, $timeout) { | |
| console.log(arguments) | |
| }) | |
| const testModule1 = createModule('testModule', function ($state, $scope, $timeout, $uibModal) { | |
| console.log(arguments) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment