0

I have this html code:

<select class="" ng-model="livre_selection" material-select multiple watch>
                <option ng-repeat="livre in livres">{{livre.titre }}</option>
            </select>

can I get the "livre_selection" in the controller and put it in a variable inside the controller so that I can use it ?

Thank you

4 Answers 4

3

Yes, AngularJS has two way data binding. whatever you put in the ng-model will be available in the controller through $scope object. in your case you can access it by $scope.livre_selection

Sign up to request clarification or add additional context in comments.

7 Comments

it worked, however when I put console.log(result) it shows the output twice , why is that ?
could you please use plunkr and post your code there?
when I modify the value, in the console it is written twice
Are you calling your controller twice? This happened to me when I was calling it in the routing template and in the markup itself. Also, this is a different issue than what you posted. Can you mark this as the answer?
@Jean I'm not sure, why this answer is not marked..The other answer is correct but that is not what you asked in the question
|
1

yes you can get that.follow the example.check ng-change and ng-click functions.you can do it by using both or only one function.

<html>
  <body ng-app="myApp" ng-controller="yourCtrl">
    <select  ng-model="livre_selection" ng-change="a();" material-select multiple watch>
    <option ng-repeat="livre in livres">{{livre.titre }}</option>
    </select>
    <input type="button" value="save" ng-click="b();">

    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>
    <script>
    var app = angular.module('myApp', []);
    app.controller('yourCtrl', function($scope) {
      $scope.livres=[{titre:"a"}]; 
      var selectedValue;

      $scope.a = function(){
        selectedValue = $scope.livre_selection;
      }

      $scope.b = function(){
        alert(selectedValue);
      }
    });   
    </script>      
  </body>    
</html>

Comments

0

Try

$scope.livre_selection = '';

Because $scope is basically a map, but more advisable yould be to use upperCase variable naming, like so:

$scope.liveReSelection = '';

After it, you will be able to access this variable in your controller.

You can read more here.

Comments

-2
  • Controller

    $scope.livre_selection = ""; <br>

    var localVariable = "";

    $scope.changeValue=function(changedVal){ localVariable = changedVal; }

  • HTML

  • Write an ng-change in the html near to the controller.
    <select class="" ng-change="changeValue(livre_selection)" ng-model="livre_selection" material-select multiple watch> <option ng-repeat="livre in livres">{{livre.titre }}</option> </select>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.