我的控制器中有以下代码
$scope.currencies = [
{ "currency_code": "USD", "currency_name": "American Dollar" },
{ "currency_code": "AUD", "currency_name": "Australian Dollar" },
{ "currency_code": "BRL", "currency_name": "Brazilian Real" },
{ "currency_code": "CAD", "currency_name": "Canadian Dollar" },
{ "currency_code": "CHF", "currency_name": "Swiss Franc" },
{ "currency_code": "CLP", "currency_name": "Chilean Peso" }
]
$scope.selectedCurrency = "USD";
$scope.doStuffs = function(){
alert($scope.selectedCurrency);
}在我看来
<div class="selected_currency">{{selectedCurrency}}</div>
<select id="currency" name="currency" ng-model="selectedCurrency"
ng-options="currency.currency_code as currency.currency_code for currency in currencies">
</select>问题
当我从select中更改货币并执行doStuffs()函数时,它会警告默认值USD,模型selectedCurrency没有接受更改的值。
然而,表达式{{selectedCurrency}}工作得很好。
当选择值发生变化时,有人能告诉我如何更新模型吗?
谢谢
更新
这不管用
<ion-view view-title="My Stuffs" ng-controller="MyCtrl">
<ion-content>
<!-- other tags -->
</ion-content>
</ion-view>但是,下面的代码起作用了(我刚刚在ng-controller标记中定义了ion-content指令)
<ion-view view-title="My Stuffs">
<ion-content ng-controller="MyCtrl">
<!-- other tags -->
</ion-content>
</ion-view>发布于 2016-06-02 06:40:07
当通过引用更新值时,双向绑定工作在角度上。在您的示例中,模型selectedCurrency是javascript中的一个原始string,不会通过引用进行更新,并且值的更改不会双向工作。
为了使其工作,将模型绑定到对象的属性:
控制器
$scope.model.selectedCurrency = 'USD';ng-model="model.selectedCurrency" https://stackoverflow.com/questions/37582495
复制相似问题