如何在控制器范围内调用更改事件?

时间:2019-05-20 11:14:18

标签: angularjs angularjs-scope

我在我的角度应用程序中调用change事件时遇到问题。我的代码结构如下:

<div class="content-area" ng-app="management">

    <div class="project-table-wrapper" ng-controller="listCtrl">
        <!-- This is the list of the project -->
        <div class="">....</div>
        <a class="btn btn-success btn-addeditproject btn-addproject" ng-click="openAddProjectDialog()" href="javascript:;">
                <i class="fa fa-plus-circle"></i>
                Add Project
        </a>
    </div>
    <div ng-controller="addCtrl">
        <div class="search-box">
            <div class="search-input-wrap">
                <input placeholder="Search"
                       type="text"
                       id="searchInput" class="searchInput"
                       data-searchFor="newPro"
                       ng-model="suvery_search" ng-change="addCtrl.liveSearch(suvery_search)" >
                <i class="fa fa-search"></i>
            </div>
            <button class="btn-search searchIt" ng-click="addCtrl.liveSearch(suvery_search)" name="searchIt" value="Go"></button>
        </div>
    </div>
</div>

控制器Js:

projectApp.controller('addCtrl', function ($scope, $window, $http, $timeout, $modal) {
    $scope.liveSearch = function(suvery_search) {
        console.log(suvery_search);
    }
});

projectApp.controller('listCtrl', function ($scope, $http, $window, $timeout) {
    ...
});

虽然我在搜索事件上触发了更改事件,但是liveSearch函数不会调用以addCtrl编写的函数。代替这个如果我写相同 listCtrl中的功能可以正常工作。

我不了解这种行为,有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

问题在于事件绑定的语法。您必须从视图中以protected virtual async Task<OutputModel> PerformActions(){ // ...do stuff here, which produces no output return null; } protected virtual async Task HandleOutput(OutputModel output) { // Do nothing, since there is no output return; } 的形式调用控制器函数,并且对于ng-change="liveSearch(suvery_search)"事件也是如此。

click
var projectApp = angular.module("management", []);

projectApp.controller('addCtrl', function ($scope, $window, $http, $timeout) {
    $scope.liveSearch = function(suvery_search) {
        console.log("addCtrl", suvery_search);
    }
});

projectApp.controller('listCtrl', function ($scope, $http, $window, $timeout) {
  $scope.liveSearch = function(suvery_search) {
        console.log("listCtrl", suvery_search);
    }
});