[英]AngularJS - array in $scope re-defined on each $scope.function call
So for my website I am trying to set up some custom functions with AngularJS. 因此,对于我的网站,我尝试使用AngularJS设置一些自定义函数。
My output is as follows: 我的输出如下:
<div data-ng-repeat="filter in menu.filters" data-ng-controller="MenuController">
<label>{{filter.title}}</label>
<input type="checkbox" data-ng-click="setSelectedFilter()" />
</div>
My controller looks like this: 我的控制器如下所示:
.controller("MenuController", ['$scope','$log', function($scope, $log) {
$scope.selectedFilters = [];
$scope.setSelectedFilter = function () {
var stub = this.filter.stub;
if (_.contains($scope.selectedFilters, stub)) {
$scope.selectedFilters = _.without($scope.selectedFilters, stub);
} else {
$scope.selectedFilters.push(stub);
};
$log.log($scope.selectedFilters);
return false;
};
}])
The problem I am having is that whenever I log $scope.selectedFilters
at the beginning of $scope.setSelectedFilter
, it's always a blank array. 我遇到的问题是,每当我登录
$scope.selectedFilters
之初$scope.setSelectedFilter
,它始终是一个空白阵列。 When I log $scope.selectedFilters
at the end of my function, it contains the value I pushed there, but it doesn't hold onto it. 当我在函数末尾登录
$scope.selectedFilters
时,它包含我在此处推送的值,但没有保留。
If I define $scope.selectedFilters
as holding several values, those values show up in place of an empty array, like the array is getting re-built from the original declaration each time my function runs. 如果我将
$scope.selectedFilters
定义$scope.selectedFilters
多个值,则这些值将显示在一个空数组的位置,就像每次函数运行时都从原始声明中重新构建该数组一样。
How do I get the $scope.selectedFilters
array to hold on to the values I push to it from the $scope.setSelectedFilters
function? 如何获取
$scope.selectedFilters
数组以保留我从$scope.setSelectedFilters
函数推入的值?
Here is a fiddle showing the problem: http://jsfiddle.net/HAz3p/ 这是显示问题的小提琴: http : //jsfiddle.net/HAz3p/
You are initializing new controller for every ng-repeat div. 您正在为每个ng-repeat div初始化新的控制器。 So the three checkboxes dont share a controller.
因此,三个复选框不共享控制器。 they have their own seperate.
他们有自己的独立。
here is the fixed code 这是固定代码
<div data-ng-controller="MenuController" >
<form>
<div data-ng-repeat="filter in menu.filters" >
<label>{{filter.title}}</label>
<input type="checkbox" data-ng-click="setSelectedFilter()" />
</div>
</form>
</div>
There are multiple answers to this small problems of yours: But the best is: Define a factory and initialize "selectedFilters" into that factory and it will be initialized only once: 对于您的小问题,有多种答案:但是最好的方法是:定义一个工厂并将“ selectedFilters”初始化到该工厂中,并且它将仅初始化一次:
.factory('myfactory', function(){
var selectedFilters = {};
return selectedFilters;
});
.controller("MenuController", ['$scope','$log', function($scope, $log, myfactory) {
$scope.selectedFilters = myfactory;
...
...
}]);
as you declare "myfactory" and returning your empty object from that factory ie selectedFilters; 当您声明“ myfactory”并从该工厂返回空对象时,即selectedFilters;
$scope.selectedFilters = myfactory;// myfactory contains the object returned from factory ans stored into the scope of the controller, now it would be initialized only once.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.