angularjs使用ng-options选择默认值

时间:2016-08-30 07:34:01

标签: javascript angularjs

第一个问题:

当我的数据不是数组时,如何使用ng-option设置默认值?如果项目是数组,我很容易做到

select = $scope.items[0];

以下是 Demo

第二个问题:

  <select ng-model="selected" ng-options="name for (name, value) in items"></select>

我实际上不知道这是怎么回事^

在我的控制器中我有像这样的键值对象

 $scope.items = {
    'one': 30,
    'two': 27,
    'three': 50,
    'four': 15,
    'five': 27,
    'six': 30
   };

2 个答案:

答案 0 :(得分:0)

的解决方案

问题1:

如果$scope.items不是数组而是object 那么

select = $scope.items.one; // to select first

OR

select = $scope.items['one']; // to select first

问题2:

代码

 <select ng-model="selected" ng-options="name for (name, value) in items"></select>

JS

$scope.secleted = $scope.items['one']

(name, value) in items将返回一个名称和值数组(key value对的数组)。您可以使用values映射name。这就是它工作的原因

答案 1 :(得分:0)

当对象中的值是基本类型时(如您的情况),angular会按值而不是引用进行比较。所以:

Information:Gradle tasks [:app:assembleDebug]
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAnimatedVectorDrawable2340Library UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72340Library UP-TO-DATE
:app:prepareComAndroidSupportCardviewV72340Library UP-TO-DATE
:app:prepareComAndroidSupportDesign2340Library UP-TO-DATE
:app:prepareComAndroidSupportRecyclerviewV72340Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42340Library UP-TO-DATE
:app:prepareComAndroidSupportSupportVectorDrawable2340Library UP-TO-DATE
:app:prepareComCouchbaseLiteCouchbaseLiteAndroid121Library UP-TO-DATE
:app:prepareComCouchbaseLiteCouchbaseLiteAndroidSqliteDefault121Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesAnalytics920Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesAnalyticsImpl920Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesBase920Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesBasement920Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesTasks920Library UP-TO-DATE
:app:prepareComJakewhartonButterknife810Library UP-TO-DATE
:app:prepareComMiguelcatalanMaterialsearchview140Library UP-TO-DATE
:app:preparePlTajchertWaitingdots020Library UP-TO-DATE
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:mergeDebugShaders UP-TO-DATE
:app:compileDebugShaders UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:incrementalDebugJavaCompilationSafeguard
:app:compileDebugJavaWithJavac
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
:app:compileDebugNdk UP-TO-DATE
:app:compileDebugSources
:app:prePackageMarkerForDebug
:app:transformClassesWithDexForDebug
To run dex in process, the Gradle daemon needs a larger heap.
It currently has approximately 910 MB.
For faster builds, increase the maximum heap size for the Gradle daemon to more than 2048 MB.
To do this set org.gradle.jvmargs=-Xmx2048M in the project gradle.properties.
For more information see https://docs.gradle.org/current/userguide/build_environment.html
Error:Error converting bytecode to dex:
Cause: com.android.dex.DexException: Multiple dex files define Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat$AccessibilityServiceInfoVersionImpl;
:app:transformClassesWithDexForDebug FAILED
Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_102\bin\java.exe'' finished with non-zero exit value 2
Information:BUILD FAILED
Information:Total time: 2 mins 52.839 secs
Information:2 errors
Information:0 warnings
Information:See complete output in console

或:

$scope.selected = 30;

作品。

如果值是非原始值,那么我建议使用&#34; track by&#34;跟踪除了对象之外的东西。