Leveraging the $scope.info variable in an Angular service

Looking to integrate a method that detects user typing behavior with a service. This method updates an info variable by setting its text to "typing" or leaving it blank.

I am considering using this method as a service for my Angular model. The challenge is that the method requires access to the text variable {{info}} which resides within the view (HTML).

How can I make this work? Here's my code snippet below...

javascript file

mymodule.controller("cntrlChat", ['$scope','isUserTypingService',
  function($scope,isUserTypingService){ 

  $scope.isUserTyping=function(){
    isUserTypingService($scope.info);

  }

}]);

mymodule.factory('isUserTypingService',['$q','$timeout', function($q,$timeout) {

  var isUserTyping= function(info) {
     runTwoFunctionWithSleepBetweenThem(function (){info='user is typing...';},function (){info='';},3500);
  };

  var runTwoFunctionWithSleepBetweenThem=function(foo1, foo2, time) {
    $q.when(foo1()).then(() => $timeout(foo2, time));
  }
  return isUserTyping;
}]);

index.html

 <html>
 <head>

    <script data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0968676e7c65687b27637a4938273d2771">[email protected]</a>" src="https://code.angularjs.org/1.4.9/angular.js" data-semver="1.4.9">
    </script>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular-route.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.3.1/angular-ui-router.min.js"></script>
    <script src="/socket.io/socket.io.js"></script>
    <script src="script.js"></script>

    <link rel="stylesheet" type="text/css" href="../css/style.css">
 </head>

 <div ng-app="mymodule" ng-view>
 </div>


 </html>

chat.html

{{info}} 

Answer №1

Here is an example of how you can achieve this:

<input ng-model="userInput" ng-change="executeOnInputChange()" />

In your controller:

$scope.userInput = '';    
$scope.executeOnInputChange = function () { performActionService($scope.userInput); }

Every time the user types something, the executeOnInputChange function will be triggered and it will in turn call your service.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to Choose Two Checkboxes from a Set of Multiple Checkboxes in AngularJS

I am looking to choose just two checkboxes from a selection of multiple checkboxes that are not nested in a list. <input type="checkbox" class="two-checkbox" id="curr-EUR" ng-model="reqObject.EUR" ng-click="checkChanged()">EUR</input> <inpu ...

Transforming a series of intervals into a condensed list

I am currently working on transforming a Legend list that was initially based on a range (1-2=blue, 2-3=red, etc.) into a flat list of words ("blah"=blue, "peek"=red). The challenge I am facing is finding a way to retain the numerical system while replaci ...

Automatically add values after successful Facebook login

Currently, I am working on a Cordova project where I have implemented Facebook login for user authentication. While the login is functioning correctly, I am facing an issue where I need to manually press a button with the ID getinfo in order for the values ...

Displaying Solidity mapping data within a React component

Check out this Solidity contract snippet: pragma solidity ^0.8.7; contract SomeContract { address public manager; uint public theNumberNine; mapping (string => mapping (address => uint)) public stringToAddrToInt; constructor() { manage ...

Is there a way to utilize the $on, $emit, and $broadcast methods when using the Controller as syntax?

It's simple to trigger an event or observe one using $scope. (function() { "use strict"; angular .module("app") .controller("Ctrl", [ "$scope", CtrlDefinition ]); function CtrlDefinition($ ...

Validation of VAT numbers using JavaScript instead of PHP

I came across an interesting function at this link that is used for checking VAT numbers in the EU. However, I am struggling to integrate it with my registration form. I would like to convert it into a JavaScript function so that I can validate the number ...

The Three.js tweened property fails to reach its target value and ends up completing the animation while remaining stuck at its original value

I've been experimenting with implementing Tween in my WebGL project at to smoothly move the camera to a new position. Unfortunately, I'm facing challenges getting Tween to function as desired. When attempting to tween the camera.position.x valu ...

React: segregate state and functions away from the view, although encountering an excess of props to transmit

Experimenting with a new approach that keeps state definition and functions separate from components. For example: // Display.js const Display = (props) => { const { data, setData, action } = props; ... return <div>...</div>; } // Di ...

What is the method for adjusting the time format?

Using the TIME data type, my data is currently displayed in the format hh:mm:ss (03:14:00). How can I change it to display in the format hh:mm (03:14)? The usual DATE type method does not seem to work: {{test.time | date: 'HH:mm'}} However, thi ...

What is the best way to navigate to a tab on a different page using rails?

Is there a way to redirect to a specific tab on another page using Rails? I am facing an issue where clicking on a button on the first page redirects me to the default tab (basic) of the destination page. However, I would like it to redirect to the notific ...

What is the best way to change a string into JSON format using JavaScript?

For a recent web project, I utilized the Django framework in conjunction with a MongoDB database. In this project, I implemented a feature where a set of data objects from the database are assigned to a combobox. Interestingly, I incorporated two comboboxe ...

Guide on modifying a field in a table when a checkbox is selected using Angular

I have a task that involves updating the fields in a database table to TRUE (boolean field) when a checkbox is checked, and FALSE when unchecked. Below is the code for the checkbox in my Angular HTML page: <checkbox-field attribute='updateExistin ...

Alert: '[Vue warning]: Directive "in testing" could not be resolved.'

Currently, I am running unit tests within a Vue 2.0 application using PhantomJS, Karma, Mocha and Chai. Although the tests are passing successfully, I am encountering a warning message with each test indicating an issue like this: ERROR: '[Vue warn ...

Error encountered: Attempted to set invalid data in DocumentReference function. Value of field is not supported: undefined

Currently in the process of developing an e-commerce platform using Vue and Firebase. I am encountering an issue when attempting to add cart information for the logged-in user. Surprisingly, the information is saved perfectly on the initial attempt. Howeve ...

Unit test: Passing a deeply nested object as a prop to a React component

My functional component looks like this: const ImageScreen = (props: any) => { const images: object[] = []; props.navigation.state.params.images.forEach((image: any) => //some code ); return ( <View style={CommonStyles.normalPage} ...

Could you please ensure that the animation activates upon hovering over the "a" element?

Utilizing bootstrap, I have created the following code. I am looking to add functionality that triggers an animation upon mouseover of an img or a element, and stops the animation upon mouseleave. .progress-bar { animation: pr 2s infinite; } @keyfr ...

Animated jQuery carousel with a timer countdown feature

Currently, I am developing a jquery slider/carousel to display various promotions. I am seeking a method to indicate the time left until the next promotion appears. Similar to the flash promo on this website: Do you have any suggestions? ...

What is the best way to make text appear as if it is floating in Jade or HTML?

Currently, I am facing an issue with a Jade file that displays an error message when a user tries to log in with incorrect credentials. The main problem is that this error message disrupts the alignment of everything else on the page, as it is just a regul ...

What is the best way to load $cookies into an Angular service?

After defining an Angular service where I need to access a cookie, I noticed that the $cookieStore is deprecated and that it's recommended to use $cookies instead. This is how my service is set up: 'use strict'; var lunchrServices = angul ...

How can I ensure my md-sidenav takes up the entire height when using ui-router?

Currently, I am utilizing the most recent version of Angular Material (1.0.0) library and I am attempting to ensure that my <md-sidebar> panel occupies the entire height of the available space on the webpage. While it functions correctly on a regul ...