【发布时间】:2016-03-08 23:22:48
【问题描述】:
我的 Angular.js 项目具有一个绑定到模型的选择元素。加载时选择了适当的选项,但是选择另一个选项似乎不会更新模型,至少不会永久更新。如果我以编程方式更改模型,但结果符合预期。
控制器:
angular.module('myApp.controllers').controller('HomeCtrl',function($scope){
$scope.month = "08"; // this default value is selected on-load as expected
$scope.updateMonth = function() {
console.log($scope.month); // this shows the original value not the newly selected value
// a http request and other things happen here...
};
$scope.logMonth = function(month) {
console.log(month); // this shows the newly selected value as expected
}
});
模板:
<select ng-model="month" ng-change="logMonth(month);">
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
</select>
<button ng-click="updateMonth();">Update Month</button>
【问题讨论】:
-
在您的模板中,
logMonth(month);存在,但您在$scope.logMonth中没有该参数...当您仅使用ng-change="logMonth()"时会发生什么? -
控制台出现错误?
-
刚刚创建了一个jsFiddle,我无法复制您的问题。
-
代码中多余的
});是复制粘贴的吗? -
ZackMacomber 和 Akis_Tfs 道歉,看起来我在编辑一些代码以使大家更容易阅读时错过了一些东西,我刚刚更新了它。 Lex 感谢您的测试,这可能表明这是周围代码的问题。
标签: javascript angularjs angularjs-scope angularjs-ng-model