0

I am developing an angularJS. Here is my code in PHP:

<label class="item item-input item-select">
    <div class="input-label">Do you have the right to work in UK?</div>
    <select name="do_you_have_the_right_to_work_in_uk" ng-model="do_you_have_the_right_to_work_in_uk">
        <option value="">Select</option>
        <option value="yes" <?php echo $user->do_you_have_the_right_to_work_in_uk == 'yes' ? 'selected' : ''; ?>>Yes</option>
        <option value="no" <?php echo $user->do_you_have_the_right_to_work_in_uk == 'no' ? 'selected' : ''; ?>>No</option>
    </select>
</label>

Considering that the variable {{user_data.do_you_have_the_right_to_work_in_uk}} holds 'yes' or 'no'. What would be the equalent of my PHP code in angularJS?

3 Answers 3

1

Actualy you don't need to add condition in option select. Instead only setting ng-model="do_you_have_the_right_to_work_in_uk" should have worked as

var app = angular.module('myApp', []);

app.controller('MainCtrl', function($scope) {
  $scope.do_you_have_the_right_to_work_in_uk =  "no"
});
  <script data-require="[email protected]" src="https://code.angularjs.org/1.4.9/angular.js" data-semver="1.4.9"></script>


<body ng-app="myApp" ng-controller="MainCtrl">
  <label class="item item-input item-select">
    <div class="input-label">Do you have the right to work in UK?</div>
    <select name="do_you_have_the_right_to_work_in_uk" ng-model="do_you_have_the_right_to_work_in_uk">
      <option value="">Select</option>
      <option value="yes">Yes</option>
      <option value="no" >No</option>
    </select>
  </label>
</body>

</html>

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

Comments

0

Instated of selected only use selected="selected". This adds selected attribute to the option.

<option value="yes" <?php echo $user->do_you_have_the_right_to_work_in_uk == 'yes' ? 'selected="selected"' : ''; ?>>Yes</option>

Comments

0

use ng-selected

<select name="do_you_have_the_right_to_work_in_uk" ng-model="do_you_have_the_right_to_work_in_uk">
  <option value="">Select</option>
  <option value="yes" ng-selected="do_you_have_the_right_to_work_in_uk === 'yes'">Yes</option>
  <option value="no" ng-selected="do_you_have_the_right_to_work_in_uk === 'no'">No</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.