我正在使用AngularJS应用程序。我试图从下拉列表中访问所选选项并在控制器中输入文本。我无法在控制器中获取访问选择选项,我也希望将所选选项与输入文本连接。
HTML
<select ng-model="countSelector"
ng-change="changeCount('{{countSelector}}')"
ng-options="country.name for country in countries">
</select>
<input type="text"
placeholder="Mobile Number"
ng-model="MobileNumber"/>
<button class="button button-block button-balanced"
ng-click="signUp(MobileNumber , countSelector)">
Verify
</button>
控制器
$scope.countries = [ {
name: "India",
dial_code: "+91",
code: "IN"
}, {
name: "Israel",
dial_code: "+972",
code: "IL"
}, {
name: "Afghanistan",
dial_code: "+93",
code: "AF"
}, {
name: "Albania",
dial_code: "+355",
code: "AL"
}, {
name: "Algeria",
dial_code: "+213",
code: "DZ"
}, {
name: "AmericanSamoa",
dial_code: "+1 684",
code: "AS"
}]
$scope.signUp = function(MobileNumber, countSelector){
var Mobile = MobileNumber;
console.log(Mobile);
var code = countSelector;
console.log(countSelector);
};
答案 0 :(得分:1)
试试这个代码段。
var myApp = angular.module('myApp',[]);
myApp.controller('GreetingController', ['$scope', function($scope) {
$scope.countries = [ {
name: "India",
dial_code: "+91",
code: "IN"
}, {
name: "Israel",
dial_code: "+972",
code: "IL"
}, {
name: "Afghanistan",
dial_code: "+93",
code: "AF"
}, {
name: "Albania",
dial_code: "+355",
code: "AL"
}, {
name: "Algeria",
dial_code: "+213",
code: "DZ"
}, {
name: "AmericanSamoa",
dial_code: "+1 684",
code: "AS"
}]
$scope.signUp = function(MobileNumber, countSelector){
var code = countSelector;
console.log(countSelector);
var Mobile = MobileNumber;
console.log(countSelector.dial_code + Mobile);
};
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
<div ng-controller="GreetingController">
<select ng-model="countSelector"
ng-change="changeCount('{{countSelector}}')"
ng-options="country.name for country in countries">
</select>
<input type="text"
placeholder="Mobile Number"
ng-model="MobileNumber"/>
<button class="button button-block button-balanced"
ng-click="signUp(MobileNumber , countSelector)">
Verify
</button>
</div>
</div>
答案 1 :(得分:0)
您可以通过ng-model="countSelector"
访问所选选项,因此只需使用$scope.countSelector
即可与$scope.MobileNumber
答案 2 :(得分:0)
访问国家/地区名称尝试这样(因为它是一个对象):
<select ng-model="countSelector"
ng-change="changeCount('{{countSelector}}')"
ng-options="country.name for country in countries">
</select>
<input type="text"
placeholder="Mobile Number"
ng-model="MobileNumber"/>
<button class="button button-block button-balanced"
ng-click="signUp(MobileNumber , countSelector.name)">
Verify
</button>
<div>{{countSelector.name}}-{{MobileNumber}}</div>