无论如何,在Visual Studio智能感知中定义一个未定义的对象
Is there anyway to define an undefined object in Visual Studio intellisense?
假设我在AngularJS中有一个控制器:
myApp.controller('SearchController',
function ($scope, UserService) {
// for intellisense, UserService is undefined here
var user = UserService.getUsers().then(function(data){
// yada yada
}, function(err){
// yada yada
});
});
但是,在我的智能感知文件中,我可以动态注入UserService以获取其功能,如下所示:
intellisense.addEventListener('statementcompletion', function (event) {
// tried doing this, but doesn't work!
// event.target = {};
var injector = angular.injector(['ng', 'myApp']);
var dependency = injector.get(event.targetName);
event.items = [];
for (method in dependency) {
intellisense.logMessage(method);
event.items.push({ name: method, kind: 'field', value: function () { } });
}
});
现在,如果我有一个全局变量(或函数变量)定义为UserService = {}
并且在我的控制器函数中键入UserService.
我将弹出服务中的所有函数。但是,如果我没有定义它,因为它被智能感知解释为undefined
,即使statementcompletion
工作,它也无法向我显示选项(如 Javascript 语言服务控制台所示)。
我的问题是,除了注释函数之外,是否还可以将UserService
定义为智能感知文件中的对象?定义event.target = {}
不起作用(请参阅上面的智能感知代码)。
一种工作方法是从具有空对象的智能感知代码中"调用"组件函数(控制器、服务等)。
我相信这可以更干净,但这是我所做的:
https://github.com/diwasbhattarai/angularjs-intellisense
作者:约翰·布莱索:https://github.com/jmbledsoe/angularjs-visualstudio-intellisense/
引用.js - 将此文件添加为工具>选项>文本编辑器>Javascript>Intellisense>引用中的引用
/// <reference path="../source/lib/assetBundle.js" />
/// <reference path="_moduleDecorator.js" />
/// <reference path="_componentDecorator.js" />
/// <reference path="../source/app/appBundle.js" />
intellisense.addEventListener('statementcompletion', function (event) {
// for core angular objects
addComponentToIntellisense('ng');
// for custom objects in application modules
for (var moduleIndex in modules) {
addComponentToIntellisense(modules[moduleIndex]);
}
function addComponentToIntellisense(module) {
var $injector = angular.injector(['ng', module]),
dependency = $injector.get(event.targetName),
dep;
if (typeof dependency === "function") dep = new dependency();
else dep = dependency;
for (var method in dep) {
event.items.push({ name: method, kind: 'field', value: dependency[method] });
}
}
});
_moduleDecorator.js - 跟踪应用中的所有模块
//_moduleDecorator
(function () {
var originalModule = angular.module;
// TODO change to array
modules = {};
var rep = false;
var count = 0;
angular.module = function () {
for (var k in modules) {
if (modules[k] === arguments[0]) {
rep = true;
break;
}
}
if (!rep) modules[count++] = arguments[0];
return originalModule.apply(angular, arguments);
};
})();
_componentDecorator.js - 使用空对象参数"调用"组件函数
(function () {
// pick all the components in all modules and initialize them for intellisense
for (var moduleIndex in modules) {
var currentModule = angular.module(modules[moduleIndex]),
queue = currentModule._invokeQueue,
// add other components such as value, provider, etc later
angularComponents = ['controller', 'factory', 'service', 'value'];
for (var i = 0; i < angularComponents.length; i++) {
var currentComponent = angularComponents[i],
originalComponentFn = currentModule[currentComponent];
currentModule[currentComponent] = (function (currentModule, queue, originalComponentFn) {
return function () {
originalComponentFn.apply(currentModule, arguments);
initializeComponents(queue);
};
})(currentModule, queue, originalComponentFn);
}
}
function initializeComponents(queue) {
var elem = queue.filter(function (element) {
var componentName = element[2][0].toLowerCase();
return (componentName.indexOf(componentName) !== -1);
});
for (var i = 0; i < elem.length; i++) {
var tempComp = elem[i][2][1],
compFunc;
// for array notation for DI
if (typeof tempComp !== "function") {
compFunc = tempComp[tempComp.length - 1];
} else {
compFunc = tempComp;
}
// 10 parameter dependencies initialization for now
compFunc({}, {}, {}, {}, {}, {}, {}, {}, {}, {});
}
}
})();
相关文章:
- 变量转换为另一个“未定义”的变量
- getDataAsJSON()在PHP中是一个未定义的函数,但许多解释如何使用JSONP的网站都说要使用它
- 所以这是说我的法定对象中有一个未定义的变量
- 为什么在这个数组的末尾会有一个未定义的
- 为什么我得到一个未定义的回报
- Router.use()需要中间件函数,但得到了一个未定义的函数
- 使用jQuery'在数组中循环;s中的每一个都会在生成字符串时在开头产生一个未定义的值
- 如果定义了值,为什么会得到一个未定义的值?使用 eval()
- 无论如何,在Visual Studio智能感知中定义一个未定义的对象
- 角度过滤器分组依据过滤器返回一个“未定义”字段
- 如果我在 Filter() 函数中使用索引,我会得到一个未定义的引用错误吗?
- 另一个未定义的JavaScript问题
- 我得到一个未定义而不是文件名
- 为什么我的 push() 方法不断在我的数组中插入一个“未定义”的元素
- addEventListener 使用 for 循环给了我一个未定义的结果
- Promise.all 在完成之前返回一个未定义和解析的数组
- Angular JS给出了一个未定义的函数错误
- 我得到一个未定义的索引
- JS说我有一个未定义的对象.真的需要一些帮助来解决这个问题吗
- Ajax 调用给了我一个未定义 - 未定义的错误