2017-03-27 5 views
0

anglejを使用し、DOMステートメントを使用していないjsファイルの文字列にテキストボックス(id = "lunch-menu"私はそれをしようとしたが、何も働かなかった。私はangularjsを初めて使っています。どうすればいいのか教えてください。anglejsを使用してjavascriptファイルの文字列にテキストボックス値を取得する方法

これは、あなたの入力に双方向結合(ng-model)を定義している私のjsファイルとhtmlファイル

(function() { 
 
\t // body... 
 
\t 'use strict'; 
 
\t angular.module('LunchCheck', []) 
 
\t .controller('LunchCheckController', LunchCheckController); 
 

 
\t LunchCheckController.$inject=['$scope']; 
 
\t function LunchCheckController($scope) { 
 

 
\t //code to get the textbox user input value into a string 
 
\t } 
 
})();
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <title>Lunch Checker</title> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
 
    <script src="LunchCheckApp.js"></script> 
 
</head> 
 
<body ng-app="LunchCheck"> 
 
    <div class="conatainer" ng-controller="LunchCheckController"> 
 
\t <h1>Lunch Checker</h1> 
 
\t <div class="form-group form-control"> 
 
      <input id="lunch-menu" type="text" placeholder="list comma separated dishes you usually have for lunch" ng-model="userInput"> 
 
     </div> 
 
     <div class="form-group"> 
 
     \t <button class="btn btn-default">Check If Too Much</button> 
 
     </div> 
 
     <div class="form-group message"></div> 
 
    </div> 
 
</body> 
 
</html>

+1

あなたのコントローラ – Edison

答えて

0

です。

今、あなたは自分のコントローラにそれにアクセスする必要があります。

function LunchCheckController($scope) { 
    //code to get the textbox user input value into a string 
    var value = $scope.userInput; 
} 
+0

に 'NG-model'値をtakeingについて、私はそれを試みたが、私は時にどのようなそれをコンソールに表示すると、私には未定義の値が与えられます。理由は分かりません –

0

(function() { 
 
\t // body... 
 
\t 'use strict'; 
 
\t angular.module('LunchCheck', []) 
 
\t .controller('LunchCheckController', LunchCheckController); 
 

 
\t LunchCheckController.$inject=['$scope']; 
 
\t function LunchCheckController($scope) { 
 
     var launchcheck=angular.element('#lunch-menu').val(''); 
 
     //var launchcheck=$scope.userInput; 
 
\t //code to get the textbox user input value into a string 
 

 
\t } 
 
})();
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <title>Lunch Checker</title> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
 
    <script src="LunchCheckApp.js"></script> 
 
</head> 
 
<body ng-app="LunchCheck"> 
 
    <div class="conatainer" ng-controller="LunchCheckController"> 
 
\t <h1>Lunch Checker</h1> 
 
\t <div class="form-group form-control"> 
 
      <input id="lunch-menu" type="text" placeholder="list comma separated dishes you usually have for lunch" ng-model="userInput"> 
 
     </div> 
 
     <div class="form-group"> 
 
     \t <button class="btn btn-default">Check If Too Much</button> 
 
     </div> 
 
     <div class="form-group message"></div> 
 
    </div> 
 
</body> 
 
</html>

+0

コンソールでlaunchcheck変数を印刷するときに、これが機能していないのはわかりません –

+0

私はここで変更してくださいvar launchcheck = angle.element( '#lunch-menu')。 –

+0

それは私にエラーを与える動作していない - エラー:[jqLit​​e:nosel] http://errors.angularjs.org/1.4.8/jqLit​​e/noselさらにDOMを使用して、私はDOMを使用せずにそれをしたい –

関連する問題