简体   繁体   English

AngularJS-在每个$ scope.function调用中重新定义$ scope中的数组

[英]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>

http://jsfiddle.net/HAz3p/1/ http://jsfiddle.net/HAz3p/1/

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.

相关问题 你如何从href调用AngularJS $ scope.Function? - How do you call an AngularJS $scope.Function from href? Karma测试对scope.function内部函数的调用 - Karma testing a call to a function inside a scope.function 从$ scope.function()内刷新$ scope - Refreshing $scope from inside a $scope.function() 控制器中定义的函数(javascript)与scope.function(angular函数)的区别 - Difference of function(javascript) vs scope.function(angular function) defined in controller Angular中$ scope.Variable和$ scope.Function之间的差异? - Differnce between $scope.Variable and $scope.Function in Angular? 通过右键单击从当前或新选项卡中的href调用Angular $ scope.Function? - Call an Angular $scope.Function from href in current or new tab by right click? AngularJS范围定义的函数为null - AngularJS scope defined function is null 如何从事件处理程序调用AngularJS范围定义的函数? - How to call an AngularJS scope-defined function from an event handler? 从html调用angularjs控制器中的函数,并且未定义范围变量 - call a function in angularjs controller from html and scope variable is not defined 角度$ scope无法正常工作。 试图在控制器$ scope和$ scope.function范围内分配对象值 - Angular $scope not working as expected. Trying to assign object values in a controller $scope and a $scope.function scope
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM