簡體   English   中英

角度:$ scope有效,但“ controller as”不會觸發ng-change事件

[英]Angular: $scope works but “controller as” does not fire ng-change event

我有一個控制器,如下所示:

(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()"/>

並且profile.html頁面的開始是: <div class="container" ng-controller="ProfilesController as controller">

使用這種方法時,輸入字段中的change事件不會在控制器中觸發。 但是從“ controller as”樣式更改為$scope可以正常工作。

我正在使用ui-router框架而不是ng-route。 (我以前開始使用的yeoman發電機為我選擇了這個)。

題:

我最近幾天才剛開始使用Angular,但是閱讀“ controller as”比較容易測試。 如何使用“控制器為”樣式進行此工作?

根據ng-change 您應該使用:

ng-change="controller.searchProfiles()"

代替:

ng-change="controller.searchProfiles"

在定義$scope.$on函數之前,您正在對其進行調用,這將導致其余代碼無法加載。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM