简体   繁体   English

将$ watch用于两个变量angularjs

[英]Using $watch for two variables angularjs

I have a pop up screen in which a user require to select from two drop down lists. 我有一个弹出屏幕,用户需要在其中从两个下拉列表中进行选择。 After the selections were completed i return the selections to the service and save them in an object. 选择完成后,我将选择返回到服务中并将其保存在对象中。

app.service('OriginalService', [ '$modal',

function ($modal) {
var that = this;

this.filtersMananger = { // my-ng-models for the two drop-down-lists
    firstFilter: "",
    secondFilter: ""
};

this.openDialog = function(){
    var modalInstance = $modal.open({
        templateUrl: 'ModalScreen.html',
        controller: 'ModalController',
        resolve: {
            filtersManagerObject: function () {
                return that.filtersMananger;
            }
        }
    });

    modalInstance.result.then(function (filtersMananger) {
        that.filtersMananger.firstFilter = filtersMananger.selectedFirstFilter;
        that.filtersMananger.secondFilter = filtersMananger.selectedSecondFilter;
    }, function () {

    });
};
}
 ]);

The pop up html: 弹出的HTML:

<div class="data-filters-container">
 <div class="data-filter">
    <label for="filter-data-drop-down">FIRST FILTER</label>
    <select name="filterDataDropDown" ng-model="filtersMananger.selectedFirstFilter" ng-options="filter.value as filter.name for filter in filterDropDownItems"></select>
</div>
  <div class="data-filter col-xs-4">
    <label for="filter-data-drop-down">SECOND FILTER</label>
    <select name="filterDataDropDown" ng-model="filtersMananger.selectedSecondFilter" ng-options="filter.value as filter.name for filter in filterDropDownItems"></select>
</div>

However, this change is important and i have to call to the controller which knows many other services to send them information regarding this change. 但是,此更改很重要,我必须致电知道很多其他服务的控制器,才能向他们发送有关此更改的信息。

In order to do it i used a watch function in the controller: 为了做到这一点,我在控制器中使用了监视功能:

 $scope.$watch('OriginalService.filtersMananger.firstFilter + OriginalService.filtersMananger.secondFilter', function (newVal, oldVal) {

        if (newVal !== oldVal) {

           DO SOME LOGIC
        }
    });

I compare between newVal and oldVal because when the app is uploaded the event is called and we enter to this function. 我在newVal和oldVal之间进行比较,因为在上载应用程序时会调用事件,然后我们进入此函数。

The problem is that the newVal is contains only the value of the secondVariable. 问题在于newVal仅包含secondVariable的值。 Is there any idea why the newVal is not contains also the first variable? 有什么主意为什么newVal不同时包含第一个变量吗?

Use $watchCollection : 使用$watchCollection

$scope.$watchCollection('[serviceName.Object.firstVariable,serviceName.Object.secondVariable]', function (newValues, oldValues) {

});

Or if you're using angular 1.3 use $watchGroup : 或者,如果您使用的是angular 1.3,请使用$watchGroup

$scope.$watchGroup(['serviceName.Object.firstVariable','serviceName.Object.secondVariable'],function(newValues, oldValues){

})

You could also use ng-change on your select. 您还可以选择使用ng-change。 The ng-change will call a function that do your logic ng-change将调用执行您的逻辑的函数

<div class="data-filters-container">
   <div class="data-filter">
      <label for="filter-data-drop-down">FIRST FILTER</label>
      <select name="filterDataDropDown"  ng-change="checkFilterOne()" ng-model="filtersMananger.selectedFirstFilter" ng-options="filter.value as filter.name for filter in filterDropDownItems"></select>
  </div>
  <div class="data-filter col-xs-4">
    <label for="filter-data-drop-down">SECOND FILTER</label>
    <select name="filterDataDropDown" ng-change="checkFilterTwo()" ng-model="filtersMananger.selectedSecondFilter" ng-options="filter.value as filter.name for filter in filterDropDownItems"></select>
</div>

It will call the function when your model will change that is like a $watch. 当您的模型更改时,它将调用该函数,就像$ watch一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM