尝试从位于另一个控制器中的一个控制器激活一个复选框 [英] Trying to activate a checkbox from a controller that lives in another controller

查看:36
本文介绍了尝试从位于另一个控制器中的一个控制器激活一个复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从另一个控制器中的一个控制器激活一个复选框.例如,我在单独的控制器下有一张名为information technology的卡,当我单击它时,我希望它路由到另一个页面,该页面具有来自另一个控制器的information technology复选框,并且我希望它在呈现页面时被选中.

I am trying to activate a checkbox from a controller that lives in another controller. For example, I have a card named information technology under a separate controller and when I click this I want it to route to another page that has a checkbox for information technology from another controller and I want it checked as it renders the page.

应用程序体系结构非常冗长,因此我在这里不包含任何代码库.但是我想知道我可以采取的方法.

The application architecture is very lengthy so I wont include any code base here. But I would like to know an approach I can take.

这是我希望逻辑存在的控制器,并将文本框标记为已选中(位于另一个控制器上).

This is the controller where I want the logic to live and to mark a text box as checked (which lives on another controller).

angular
  .controller("mycontroller", mycontroller);
  mycontroller.$inject = [
        "$scope"
    ];

    // getting the getData() data
        $scope.getData = function (data, type) {
            console.log("whats this data about in getData(data) ", data)
            $scope.query = data.name;
            if (data.checked == undefined) {
                data.checked = true;
            }
        }

下图:复选框控制器所在的控制器是

Below: Is the controller where the checkbox controller lives

angular
    .controller('supplierIntelligenceCtrl', function ($scope, $q, FetchData, dataStore, SharedService,
        $document, $window, $state, $rootScope, $timeout, DataCache,
        $filter, $interval, $localStorage, $http) {

                $scope.getData = function (data, type) {
            console.log("whats this data about in getData(data) ", data)
            $scope.query = data.name;
            if (data.checked == undefined) {
                data.checked = true;
            }


        }

        $scope.apply = function (type) {
            $scope.select = false;
            $scope.bigres = 0;
            $scope.mobFil = 3;
            $scope.applyFilter(type);
        }

        $scope.disableApply = false;
        $scope.disableApply2 = false;

        $scope.applyFilter = function (type) {
            console.log("this is type ", type)
            if (type == 'industries') {
                $scope.filters.industries = $scope.industries.filter(function (e) {
                    console.log("this is e ", e.checked)
                    return e.checked;
                }).map(function (f) {
                    console.log(" this is f >>>> ",
                        f)
                    return f.id
                })

                $scope.filters.countries = [];
                if ($scope.countries != undefined) {
                    $scope.countries = $scope.countries.map(function (e) {
                        e.checked = false;
                        return e;
                    })
                }
                $scope.filters.cities = [];
                if ($scope.cities != undefined) {
                    $scope.cities = $scope.cities.map(function (e) {
                        e.checked = false;
                        return e;
                    })
                }
                $scope.start = 0;
                if ($scope.filters.industries.length > 0) {
                    $scope.callBackend();
                    $scope.disableApply2 = true;
                    FetchData.fetchDNBCountriesByIndustries('industries=' + $scope.filters.industries + '&size=').then(function (res) {
                        $scope.disableApply2 = false;
                        $scope.countries = res.data;
                        $scope.countriesPage += 10
                    }, function () {
                        $scope.disableApply2 = false;
                    });
                } else {
                    $scope.callBackend();
                }
            }
            if (type == 'countries') {

                $scope.filters.countries = $scope.countries.filter(function (e) {
                    return e.checked;
                }).map(function (f) {
                    return f.id;
                })
                $scope.filters.cities = [];
                if ($scope.cities != undefined) {
                    $scope.cities = $scope.cities.map(function (e) {
                        e.checked = false;
                        return e;
                    })
                }
                $scope.start = 0;
                if ($scope.filters.countries.length > 0) {
                    $scope.callBackend();
                    $scope.disableApply2 = true;
                    FetchData.fetchDNBCitiesByIndustriesAndCountries('industries=' + $scope.filters.industries + '&countries=' + $scope.filters.countries + '&size=').then(function (res) {
                        $scope.disableApply2 = false;
                        $scope.cities = res.data;
                    }, function () {
                        $scope.disableApply2 = false;
                    })
                } else {
                    $scope.callBackend();
                }
            }
            if (type == 'cities') {
                $scope.filters.cities = $scope.cities.filter(function (e) {
                    return e.checked;
                }).map(function (f) {
                    return f.id
                })
                $scope.start = 0;
                $scope.callBackend();
            }


            if (type == 'classifications') {
                $scope.filters.classifications = $scope.classifications.filter(function (e) {
                    return e.checked;
                }).map(function (f) {
                    return f.statusCode;
                })
                $scope.start = 0;
                $scope.callBackend();
            }
        }

        }

以下是该复选框所在的HTML:

Here is the HTML where the checkbox lives:

<div ng-repeat="data in industries  ">                                 
   <input id="{{data.id}}in" type="checkbox" aria-invalid="false"
          ng-model="data.checked"
          ng-change="getData(data,'industry')">
  <label for="{{data.id}}in">{{data.name}}</label>
</div>

也许我在这里没有讲到重点,也许正在忽略某些内容.我是angularjs的新手,需要实现此功能,才能将按钮/卡片路由到另一个检查复选框过滤器的页面.

Maybe Im missing the point here and perhaps am overlooking something. Im new to angularjs and need to implement this capability to route a button/card to another page that checks a checkbox filter.

请-任何建议都很好. :)

Please - any advise would be great . :)

推荐答案

这里是一个示例,该示例说明了控制器通过依赖项注入程序注入的共享服务共享数组的示例.选中其中一个控制器中的复选框,并在另一个中显示它.

Here is an example of controllers sharing an array via a shared service injected by the dependency injector. Check the checkbox in one controller and it shows in the other.

angular.module('app', []);

angular.module('app')
  .factory('dataService', [function () {
    return {
      data: [
        { prop: '1', checked: false },
        { prop: '2', checked: false },
        { prop: '3', checked: false },
        { prop: '4', checked: false }
      ]
    };
  }]);

angular.module('app')
  .controller('controller1', ['dataService', function (dataService) {
    this.data = dataService.data;
  }]);

angular.module('app')
  .controller('controller2', ['dataService', function (dataService) {
    this.data = dataService.data;
  }]);
  
angular.module('app')
  .controller('controller3', ['dataService', function (dataService) {
    this.toggleAll = () => {
      dataService.data.forEach(item => item.checked = !item.checked)
    };
  }]);

[ng-controller] { display: inline-block; margin-right: 30px; vertical-align: top; }

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.2/angular.min.js"></script>
<div ng-app="app">
  <div ng-controller="controller1 as ctrl">
    <strong>Controller 1</strong>
    <div ng-repeat="item in ctrl.data">
      <label>Item {{item.prop}} <input type="checkbox" ng-model="item.checked"></label>
    </div>
  </div>
  
  <div ng-controller="controller2 as ctrl">
    <strong>Controller 2</strong>
    <div ng-repeat="item in ctrl.data">
      <label>Item {{item.prop}} <input type="checkbox" ng-model="item.checked"></label>
    </div>
  </div>
  
  <div ng-controller="controller3 as ctrl">
    <strong>Controller 3</strong>
    <div>
      <button ng-click="ctrl.toggleAll()">Toggle all</button>
    </div>
  </div>
</div>

这篇关于尝试从位于另一个控制器中的一个控制器激活一个复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆