簡體   English   中英

如何使用angularjs將文本框值轉換為javascript文件中的字符串var

[英]how to get a textbox value into a string var in javascript file using angularjs

我想使用angularjs但不使用任何DOM語句將textbox(id =“ lunch-menu”)值轉換為我js文件中的字符串。 我嘗試這樣做,但是沒有任何效果。 我是angularjs的新手。 請幫助我該怎么做。

這是我的js和html文件

 (function () { // body... 'use strict'; angular.module('LunchCheck', []) .controller('LunchCheckController', LunchCheckController); LunchCheckController.$inject=['$scope']; function LunchCheckController($scope) { //code to get the textbox user input value into a string } })(); 
 <!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"> <h1>Lunch Checker</h1> <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"> <button class="btn btn-default">Check If Too Much</button> </div> <div class="form-group message"></div> </div> </body> </html> 

您已經在輸入中定義了雙向綁定( ng-model )。

現在,您只需要在控制器中訪問它:

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

 (function () { // body... 'use strict'; angular.module('LunchCheck', []) .controller('LunchCheckController', LunchCheckController); LunchCheckController.$inject=['$scope']; function LunchCheckController($scope) { var launchcheck=angular.element('#lunch-menu').val(''); //var launchcheck=$scope.userInput; //code to get the textbox user input value into a string } })(); 
 <!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"> <h1>Lunch Checker</h1> <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"> <button class="btn btn-default">Check If Too Much</button> </div> <div class="form-group message"></div> </div> </body> </html> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM