Geee
Geee

Reputation: 2249

How to call change event within controller scope?

Im facing issue while call the change event in my angular App. I have an code structure below:

<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>

Controller 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) {
    ...
});

While I trigger change event on search event, liveSearch function wont calling written in the addCtrl. Instead of this If I write same function in the listCtrl this works fine.

I don't understand this behaviour, can anyone help?

Upvotes: 0

Views: 508

Answers (1)

Sajal
Sajal

Reputation: 4401

The issue is with the syntax of the event binding. You have to call the controller function from the view as ng-change="liveSearch(suvery_search)" and similarly for the click event.

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);
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<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 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="liveSearch(suvery_search)" >
                <i class="fa fa-search"></i>
            </div>
            <button class="btn-search searchIt" ng-click="liveSearch(suvery_search)" name="searchIt" value="Go">Go</button>
        </div>
    </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="liveSearch(suvery_search)" >
                <i class="fa fa-search"></i>
            </div>
            <button class="btn-search searchIt" ng-click="liveSearch(suvery_search)" name="searchIt" value="Go">Go</button>
        </div>
    </div>
</div>

Upvotes: 1

Related Questions