Skip to content

Instantly share code, notes, and snippets.

@yue1123
Last active April 19, 2024 15:59
Show Gist options
  • Select an option

  • Save yue1123/b0af4333ef9f03bc307d7dfcb7576805 to your computer and use it in GitHub Desktop.

Select an option

Save yue1123/b0af4333ef9f03bc307d7dfcb7576805 to your computer and use it in GitHub Desktop.
Angular.js any order function params Inject
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)
}
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