当绑定变量改变时,Angularjs选择不更新
angularjs select not updating when bound variable changes
好的,我有一个绑定到控制器范围变量的选择标记。当应用程序初始化时,变量为空,因此生成的选择列表只有一项——空项。
在run函数中,列表被初始化,事件被广播。控制器获取广播并使用98个值更新变量。然而,select标签似乎没有得到更新。
我做错了什么?
HTML: <select id='srcList' size='10'
data-ng-model="data.source"
data-ng-options="s.title for s in data.srcList"></select><br>
JS:
.controller('rightSidebar', function($scope, dataProvider) {
$scope.data = {};
$scope.data.srcList = dataProvider.srcList;
$scope.data.citeList = dataProvider.citeList;
$scope.$on('SrcListRefresh', function () {
$scope.data.srcList = dataProvider.srcList;
});
$scope.$on('CiteListRefresh', function () {
$scope.data.citeList = dataProvider.citeList;
});
});
我跟踪了代码,我确实看到了作用域变量数据。
所以,我不确定如何从module.run方法发出事件,因为这听起来就像你说你在做什么。
在不知道更多关于你的应用程序的情况下,我已经做了一个看起来工作良好的jsfiddle。有可能我误解了你的问题,但如果没有,你应该能够比较一下这个方法来解决它。
http://jsfiddle.net/fMtv3/function rightSidebar($scope, dataProvider) {
$scope.data = {};
$scope.data.srcList = dataProvider.srcList;
$scope.data.citeList = dataProvider.citeList;
$scope.$on('SrcListRefresh', function () {
alert("Controller Scope Level - Handler SrcListRefresh");
$scope.data.srcList = dataProvider.srcList;
});
$scope.$on('CiteListRefresh', function () {
$scope.data.citeList = dataProvider.citeList;
});
}
var myapp = angular.module("app", [], function ($routeProvider, $locationProvider) {});
myapp.run(function ($rootScope) {
$rootScope.data = {
srcList: null,
citeList: null
};
//setTimeout the non angular way
setTimeout(function () {
$rootScope.$apply(function () {
$rootScope.data.srcList = [{
title: 'A'
}, {
title: 'B'
}, {
title: 'C'
}];
});
}, 1000);
});
angular.element("#apphost").ready(function () {
angular.bootstrap("#apphost", ['app']);
});
html代码
<div id="apphost">
<select id='srcList' size='10' data-ng-model="data.source" data-ng-options="s.title for s in data.srcList"></select>
<br> </div>
我认为问题是我正在访问异步的indexedDB函数,需要$apply()调用
相关文章:
- AngularJS-选择选项并在函数中使用它
- Angularjs选择以选择数据传递
- AngularJS选择更改触发器
- AngularJS选择是必需的
- AngularJS-选择,设置编辑/新建的默认值
- AngularJS:选择不绑定到模型
- AngularJS选择检查数据并插入
- AngularJS 选择在选择值后清除
- Angularjs 选择选项 ng 选择为分配值选择空白选项
- AngularJS选择的插件,选择:更新不起作用,在浏览器中工作
- 将子元素打印为 AngularJS 选择的 ng-选项
- AngularJS选择带有ng-model和ng-repeat的默认值,而不是ng-option
- AngularJS - 选择 - 服务器调用的默认值
- AngularJs 选择 ng 选项不起作用
- Angularjs 选择而不选择
- AngularJs:选择在使用ng-repeat时不显示任何选项的下拉列表
- 在第一个由 angularjs 选择中排序默认选项
- AngularJS选择元素值是一个对象,为什么
- AngularJS:选择不会更改绑定范围变量时的选定选项
- AngularJS<选择>来自AJAX调用的<选项>的双向数据绑定