2016-07-08 9 views
0

サービスでこのメソッド(ユーザーが入力している場合はメッセージを出力します)を使用します。メソッドは情報をvaribleに更新します(テキストを "typing"または空白に設定します)。

この方法を私の角形モデルのサービスとして使用することを考えました。問題は、このメソッドは、varible {{info}}のテキストにビュー(いくつかのhtml)内に座ってアクセスする必要があるということです。

どうすればいいですか? 私のコードは おかげ

jsがファイル....以下である

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="[email protected]" 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}} 

答えて

1

あなたはこのような何かを行うことができます:

お使いのコントローラで
<input ng-model="userInput" ng-change="runWhenTyping()" /> 

$scope.userInput = '';  
$scope.runWhenTyping = function() { isUserTypingService($scope.userInput) } 

彼らはrunWhenTypingが呼び出され、順番に、それはあなたのサービスを呼び出します入力するたびに。

+0

isUserTypingService($ scope.userInput)$ scope.userInputを参照の値として渡しますか? – Matoy

+0

はいこれは参照として渡されるため、サービスで変更することができます –

関連する問題