【发布时间】:2014-08-10 01:55:18
【问题描述】:
我正在尝试为我的程序实现一种方法来跟踪用户选择和搜索的过滤器。输入将在一个文本框中,多个文本框。我有每个选项的文本框(TimeStamp、SourceHost、UUID 等)[我正在构建一个内部应用程序]。我需要能够为我的参数携带过滤器列表,我决定使用 Array 对象,但我遇到了一些问题。我初始化了我的数组,但无法访问其他函数中的信息
app.controller("AppCtrl", function($http, $scope){
var app= this;
$scope.filterList = [];
$scope.filterList.push("test");
console.log($scope.filterList);
$scope.incData = function(page, filterList){
callingIrrelevantFunction(page)
console.log($scope.filterList);
第一个 console.log 输出“test”。但是当我稍后使用它时(这不是我的预期用例,我只是想确保我可以确定一个数组的范围并在函数内部调用它)输出不正确。实际预期的用例是我将其附加到 url 的末尾。
编辑: 这是我的代码的可共享版本:
app.controller("AppCtrl", function($http, $scope){
var app = this;
$scope.filterList = [];
$scope.filterList.push("uuid=9022");
$scope.filterList.push("source_host=Kani");
function foobar(filterList){
console.log("inside foobar");
console.log(filterList);
}
foobar($scope.filterList);
});
输出“[]”。
编辑#2:
当将我的代码更改为一个数组时,没有“$scope”。在它前面,代码有效。但我相信我确实需要“$scope”。让它与角度指令一起工作。
【问题讨论】:
-
来自您的代码。我不明白为什么你的数组不会有相同的内容。您可能有其他功能来调整内容。此处未显示的部分代码。
-
呼应@DayanMorenoLeon,你能发布你的整个控制器吗?
-
callingIrrelevantFunction(page)对 $scope.filterList 做了哪些更改?。 -
为什么在“可共享”版本中将字符串分配给
$scope.filterList.push? -
@Tacoman667 我相信这是我的错误,我看过了,谢谢你指出这一点。我相信现在一切正常。
标签: javascript arrays angularjs angularjs-scope scope