我有这个包含jQuery mobile flipswitch小部件的angularjs指令:
mcb.directive('jSwitch', function () {
return {
scope: {
id: '@',
label: '@',
value: '=',
disabled: '@',
mini: '@',
switch: '&',
offlabel: '@',
onlabel: '@',
onval: '=',
offval: '='
},
restrict: 'A',
replace: false,
templateUrl: 'tpl/widget/jswitch.htm',
compile: function (e) {
e.trigger('create');
return {
post: function (s, e, a) {
s.$watch('value', function () {
return e.find('#' + a.id).flipswitch('refresh');
});
}
}
}
};
});
<label for="{{id}}">{{label}}</label>
<select id="{{id}}"
name="{{id}}"
data-role="flipswitch"
data-ng-model="value"
data-mini="{{mini}}">
<option value="{{offval}}">{{offlabel}}</option>
<option value="{{onval}}">{{onlabel}}</option>
</select>
一旦模型值发生变化,我希望它能刷新我的应用程序上绑定到同一模型的所有flipswitch小部件。
此代码确实有效,但return e.find('#' + a.id).flipswitch('refresh');
行导致angularjs摘要错误:
Error: [$rootScope:inprog] http://errors.angularjs.org/1.2.27/$rootScope/inprog?p0=%24digest
at Error (native)
at http://d.h/mcb/js/lib/ajs/ajs.js:6:450
at k (http://d.h/mcb/js/lib/ajs/ajs.js:106:41)
at h.$apply (http://d.h/mcb/js/lib/ajs/ajs.js:113:301)
at HTMLSelectElement.<anonymous> (http://d.h/mcb/js/lib/ajs/ajs.js:208:36)
at HTMLSelectElement.m.event.dispatch (http://d.h/mcb/js/lib/jquery/jquery.js:3:8440)
at HTMLSelectElement.r.handle (http://d.h/mcb/js/lib/jquery/jquery.js:3:5150)
at Object.m.event.trigger (http://d.h/mcb/js/lib/jquery/jquery.js:3:7541)
at HTMLSelectElement.<anonymous> (http://d.h/mcb/js/lib/jquery/jquery.js:3:15408)
at Function.m.extend.each (http://d.h/mcb/js/lib/jquery/jquery.js:2:2973)
我知道错误是由flipswitch('refresh')
导致的,至少我是这么认为的,因为如果我删除它,错误消失,但我失去了所需的功能。