Angularjs 意外的ng更改行为
查看中的一个练习,我正在尝试实现Angularjs 意外的ng更改行为,angularjs,Angularjs,查看中的一个练习,我正在尝试实现ng change行为,以便在对story.status进行更改时更新select的下拉值 JS $scope.setCurrentStatus = function(status) { if(typeof $scope.currentStory !== 'undefined') { $scope.currentStory.status = status.name; } }; $scope.setCurrentStory = f
ng change
行为,以便在对story.status进行更改时更新select
的下拉值
JS
$scope.setCurrentStatus = function(status) {
if(typeof $scope.currentStory !== 'undefined') {
$scope.currentStory.status = status.name;
}
};
$scope.setCurrentStory = function(story) {
$scope.currentStory = story
$scope.currentStatus = $scope.statusesIndex[story.status];
$scope.currentType = $scope.typesIndex[story.type];
console.log("$scope.currentType", $scope.currentType);
};
请注意,当我选择一个故事时,将执行setCurrentStory
中的日志记录,但不会执行setCurrentStatus
HTML
<div class="control-group">
<label class="control-label" for="inputType">Type</label>
<div class="controls">
<select id="inputType"
ng-model="currentType"
ng-options="t.name for t in types"
ng-change="setCurrentType(currentType)"></select>
</div>
</div>
</form>
类型
Fiddle-您正试图设置一个对象的属性,该属性具有null
值。如果在控制台中查看,您将看到以下错误TypeError:无法将属性“status”设置为null
。如果更改此行$scope.currentStory=null
到这个$scope.currentStory={}代码>它似乎正确设置了status属性.Hmm的值,将第4行更新为$scope.currentStory={}代码>没有为我解决此问题。抱歉,我没有理解您的问题,但是在更改状态选择的值时,抛出了一个错误。我不确定更改基础值时是否会触发ng change
。如果我理解正确,只有当用户实际更改选择时,ng change
功能才会启动。我想知道为什么我没有看到这个错误。您所做的只是更改了select的下拉值,然后您看到了错误?