简体   繁体   中英

How to call a plain js function int angular component event call back?

I have an onChange event binding in my angular component, which works fine as long I give it a function of an other angular controller. However if I pass an ordinary js function nothing is called.

This is my angular template:

<search-input on-change='$ctrl.updateDetected(value)'></search-input>

And this is the the other controller:

import angular from 'angular';
import htmlTemplate from './mainWindow.html';
import searchInputComponent from './components/searchInput/searchInput';

const moduleName = 'woodstore';

var module = angular.module(moduleName, []);

class MainWindowController {
    updateDetected(searchText) {
        console.log('In controller: '+searchText);
    }
}

module
    .component('mainWindow', {
        template: htmlTemplate,
        controller: MainWindowController
    })
    .component('searchInput', searchInputComponent);


function updateDetectedFunction(value) {
    console.log('In function: '+value);
} 

This prints In controller: Text to search for on the console. If I change the template to

<search-input on-change='updateDetectedFunction(value)'></search-input>

nothing happens.

How can I use the updateDetectedFunction() for the call back?

I don't think plain functions could be called in template, angular html template always bind to a scope, any functions in template would be eval in the context of that scope, I think only thing you could do is calling the plain function within a scope function:

<search-input on-change='$ctrl.updateDetected(value)'></search-input>

class MainWindowController {
     updateDetected(searchText) {
            updateDetectedFunction(searchText)
     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM