当下拉列表更改时,在控制器内部调用函数

时间:2015-08-31 17:54:02

标签: javascript angularjs

我的Angular.JS控制器文件中包含以下代码:

JBenchApp.controller('CaseListCtrl', ['$scope', '$http', 
  function ($scope, $http) {
      // Case list stuff here
          $http.get('http://10.34.34.46/BenchViewServices/api/Calendar/LA/5/08-27-2015').success(function (response) {
              $scope.cases = response;
          });

          $http.get('http://10.34.34.46/BenchViewServices/api/CourtDept/LA').success(function (response) {
              $scope.departments = response;
          });
  }]);

案例变量包含所选法庭(部门)的事件列表。我想要的是改变这一点,以便我可以在UI中的内容发生变化时刷新案例。例如,部门是下拉列表的一部分,当我更改部门时,我想更改案例变量以包含新选择的法庭(部门)的事件。另外,我有一个日期选择器,当它发生变化时,我希望case变量包含当前所选法庭(部门)新选择日期的通风口。

编辑在这里开始:我知道我可以使用ng-change绑定到这样的$ scope函数:

JBenchApp.controller('CaseListCtrl', ['$scope', '$http', 
  function ($scope, $http) {
      // Case list stuff here
      $scope.getCalendar = function () {
          $http.get('http://10.34.34.46/BenchViewServices/api/Calendar/LA/5/08-27-2015').success(function (response) {
              $scope.cases = response;
          });
      }

          $http.get('http://10.34.34.46/BenchViewServices/api/CourtDept/LA').success(function (response) {
              $scope.departments = response;
          });
  }]);

但是,我也理解我需要一个ng模型才能使用ng-change。然后提出两个问题:

  1. 我如何设置部门模型,因为它们也是动态加载的?我现在把它设置为$ scope.departments ...是正确的吗?
  2. 如何在日历屏幕加载时初始加载日历内容?

3 个答案:

答案 0 :(得分:0)

你在下拉列表中设置ng-change,如下所示:

<select ng-change="updateCase()">

然后你可以在你的控制器中创建一个函数:

$scope.updateCase = function(){
    var filter = //your logic...
     $scope.cases = filter;
}

答案 1 :(得分:0)

您可以使用ng-change制作,也可以在控制器中使用$watch

答案 2 :(得分:0)

您可以使用ng-change指令

这样的东西
<input ng-model = 'someModel' ng-change = 'someChangeFunction()' />

现在,在您的控制器中,您可以拥有类似的内容 -

$scope.someChangeFunction = function()
{
   //code to change cases model data
}

您应该更改AngularJS documentation - ngChange

相关问题