AngularJs ng-options(ke,value)ng-model =" selected"不起作用

时间:2016-10-01 10:21:41

标签: javascript angularjs

这是我的Html:

<select ng-options="value.name for (key, value) in countries track by key" ng-model="selected" >
</select>

这是我试图解决的问题:

$scope.countries = {
    "AFG":{"name":"Afghanistan"},
    "ALB":{"name":"Albania"}
};
$scope.countriesKeys = Object.keys($scope.countries);
$scope.selected = ????;

我的问题是我无法设法选择ng-model,对象的结构会让人感到困难......(无法改变对象)。< / p>

最后,我的目的是让<select>使用第一个选定的选项"ALB":{"name":"Albania"}并使其成为动态,这样当我按其他选项进行$scope.selected更改时。

3 个答案:

答案 0 :(得分:1)

您可以尝试以下行:

$scope.selected={"name":"Albania"}

答案 1 :(得分:0)

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

//controller
app.controller('MainCtrl', function($scope) {
    $scope.countries = {
        "AFG":{"name":"Afghanistan"},
        "ALB":{"name":"Albania"}};
        $scope.countriesKeys=Object.keys($scope.countries);
        $scope.selected="ALB";
  
        $scope.changeSelected = function(newSelected){
            $scope.selected=newSelected;
        }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp">

  <div class="div1" ng-controller="MainCtrl">
    {{selected}}
    <select ng-options="key as value.name for (key, value) in countries" ng-change="changeSelected(selected)"  ng-model="selected"></select>
  </div>

</body>

答案 2 :(得分:0)

getKeyByValue($scope.selected,$scope.countries)

我已将此功能添加到我的控制器

<option>

现在我有来自 package com.example.deepak.imageuploaddownload; import android.content.Intent; import android.net.Uri; import android.provider.MediaStore; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.Switch; import static com.example.deepak.imageuploaddownload.R.id.imageToUpload; import static com.example.deepak.imageuploaddownload.R.id.imageToUploadTwo; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private static final int RESULT_LOAD_IMAGE =1; private static final int RESULT_LOAD_IMAGETWO =1; ImageView imageUpload, imageUploadTwo,imageDownlod; Button btnUplod, btnUplodTwo,btnDownload; EditText etUploadname ,etUploadnameTwo,etDownloadname; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageUpload =(ImageView)findViewById(imageToUpload); imageUploadTwo =(ImageView)findViewById(imageToUploadTwo); imageDownlod =(ImageView)findViewById(R.id.imageToDownLoad); btnUplod=(Button)findViewById(R.id.btnUploadImage); btnUplodTwo=(Button)findViewById(R.id.btnUploadImageTwo); btnDownload=(Button)findViewById(R.id.btnDownloadImage); etUploadname=(EditText)findViewById(R.id.etUploadName); etUploadnameTwo=(EditText)findViewById(R.id.etUploadNameTwo); etDownloadname=(EditText)findViewById(R.id.etDownloadName); imageUpload.setOnClickListener(this); imageUploadTwo.setOnClickListener(this); btnUplod.setOnClickListener(this); btnUplodTwo.setOnClickListener(this); btnDownload.setOnClickListener(this); } @Override public void onClick(View v) { switch(v.getId()){ case R.id.imageToUpload: Intent gallryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(gallryIntent,RESULT_LOAD_IMAGE); break; case R.id.imageToUploadTwo: Intent gallryIntentT = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(gallryIntentT,RESULT_LOAD_IMAGETWO); break; case R.id.btnUploadImage: break; case R.id.btnDownloadImage: break; default: break; }} @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null) { Uri selectedImage = data.getData(); imageUpload.setImageURI(selectedImage); } if (requestCode == RESULT_LOAD_IMAGETWO && resultCode == RESULT_OK && data != null) { Uri selectedImagee = data.getData(); imageUploadTwo.setImageURI(selectedImagee); } } } 的价值和密钥 感谢每一个试图提供帮助的人  主要是我的问题是我添加了“item as value.name ...”