Angular:$ scope工作但"控制器为"不会触发ng-change事件

时间:2016-04-11 22:55:48

标签: javascript angularjs angular-ui-router

我有一个控制器如下:

(function () {
  'use strict';

  angular
    .module('vweb')
    .controller('ProfilesController', ProfilesController);

  /** @ngInject */
  function ProfilesController(profileService, $scope) {
    var vm = this;
    vm.searchTerm = '';
    vm.profileService = profileService;

    $scope.$on('$viewContentLoaded',
      function () {
        vm.searchProfiles();
      });

    vm.searchProfiles = function() {
      alert("term: " + vm.searchTerm);
      profileService.searchProfiles(vm.searchTerm)
        .then(function (profiles) {
          alert("Here's my profiles " + angular.toJson(profiles));
        });
    }

  }
})();

连同输入字段(搜索字词):

<input type="text" class="search-query form-control" placeholder="Search" 
ng-model="controller.searchTerm" ng-change="controller.searchProfiles()"/>

profiles.html页面的开头是:<div class="container" ng-controller="ProfilesController as controller">

使用此方法时,输入字段上的更改事件不会在控制器中触发。但是从&#39;控制器改为&#39;样式$scope工作正常。

我使用ui-router框架而不是ng-route。 (我曾经开始的自耕农生成器为我选择了这个)。

问题:

我在最近几天刚开始有角度,但是读到控制器为&#39;更容易测试。如何使用&#39;控制器作为&#39;风格?

2 个答案:

答案 0 :(得分:1)

According to ng-change. You should use:

ng-change="controller.searchProfiles()"

instead of:

ng-change="controller.searchProfiles"

答案 1 :(得分:1)

$scope.$on函数定义之前,您正在调用searchProfiles,导致其余代码无法加载。

相关问题