How can I pass variables from options to my controller?


Sam Bush

Can someone tell me how to collect the values? Please select on.change in the drop down box and use the value as a variable in the controller. For some reason the $ColorPicked variable won't be in $scope.base=response.data.type. $ColorPicked is rotated; however, if I just delete $ColorPicked and enter Red, it works fine.

I'm pretty new to Angular and JS, so please excuse my stupidity. I've googled and googled for a definitive answer, but I've found nothing.

INDEX.HTML
<h3>Color:</h3>
<select class="formBoxes" id="ColorPicked">
<option value="Red">Redd</option>
<option value="Blue">Blue</option>
<option value="Green">Green</option>
</select>

SCRIPT.JS
$ColorPicked = "Red";

var app = angular.module("computer",['ngRoute'])
.config(['$routeProvider', function($routeProvider){
  $routeProvider.
    when('/main',{
       templateUrl: 'archetype.html',
      controller:'MainCtrl'
    }).
    otherwise({redirectTo:'/main'})
}])

.controller('MainCtrl', ['$scope', '$http', function($scope, $http){
  $http.get('archetype.json').then(function(response){

    $scope.base = response.data.type.$ColorPicked;

    ;
  });
}]);
Sajeethharan

There are two things,

(i) You need to define a $scope variable for this ColorPickedto provide a default selection.

(ii) You need to have ng-model bound to a variable. Use directive to pass selected color to functionng-change

HTML:

 <select ng-model="ColorPicked" ng-change="setItem(ColorPicked)">
      <option>Red</option>
      <option>Blue</option>
      <option>Green</option>    
  </select>

Controller:

function MyCtrl($scope) {
    $scope.ColorPicked = "Red";    
    $scope.setItem = function(opt) {
    $scope.base =opt;
  }
}

DEMO

Working Demo for your requirement

Related


How can I pass php variables from my select to jquery?

Liliymas I have next code to load all countries on my website: $(document).ready(function() { $.ajax({ type: 'GET', url: '/comunidades', success: function(response) { $("#cbx_comunidad").empty(); // console.log(response); $("#c

How can I pass variables from my view to my controller?

Luke Dangerous Kozorowski I have generated a dropdown <select>on my view page with data from a stored procedure. Using JavaScript, I accessed the selection <option>and saved the text into a variable. Now I need to send that variable back to my controller so I

How can I pass system property options to my jar file?

Rogman I have written a small application to parse large XML files using SAX with Intellij . -DentityExpansionLimit=0Pass options to my application by going to "Run\Edit Configurations..." and setting VM options . When I run the application with Intellij it wo

How can I pass php variables from my select to jquery?

Liliymas I have next code to load all countries on my website: $(document).ready(function() { $.ajax({ type: 'GET', url: '/comunidades', success: function(response) { $("#cbx_comunidad").empty(); // console.log(response); $("#c

How can I get my ID from the button to my controller?

Buzken What am I doing wrong? I want to add an order to an existing customer, but the customer's id doesn't match the controller. public ActionResult AddOrder(Guid? customerId) { if (customerId == null) { throw new ArgumentNullException(nameof(

How can I pass python variables to my template via flask?

Eric Nagy I made a web app with flask. I want to pass a variable value to my template to set a property. In python: web_size="100%" @app.route('/') def home(): return render_template("web.html") def size(): return "50px" In the html5 file I used this: w

How can I pass python variables to my template via flask?

Eric Nagy I made a web app with flask. I want to pass a variable value to my template to set a property. In python: web_size="100%" @app.route('/') def home(): return render_template("web.html") def size(): return "50px" In the html5 file I used this: w

How can I pass variables from my post to my get?

programmer man I am using react/express. I have a simple web page that takes user input and uses axios.post. In my backend, I need to create a file in my get. I haven't been able to get the return function to work, I don't want to use a global variable as it h

How can I pass php variables from my select to jquery?

Liliymas I have next code to load all countries on my website: $(document).ready(function() { $.ajax({ type: 'GET', url: '/comunidades', success: function(response) { $("#cbx_comunidad").empty(); // console.log(response); $("#c

How can I pass system property options to my jar file?

Rogman I have written a small application to parse large XML files using SAX with Intellij . -DentityExpansionLimit=0Pass options to my application by going to "Run\Edit Configurations..." and setting VM options . When I run the application with Intellij it wo