Angular指令无法确定ng模型的范围
Angular directive can not scope ng-model
我正试图使用控制器$scope将现有值传递给DOM,用现有值填充表单。现在,我正在使用一个指令来读取和写入特定选择菜单(在这里我使用引导表单助手)中的值:
app.directive('currencypicker', function () {
return {
restrict: 'A',
require: '?ngModel',
scope: {
ngModel: '='
},
link: function (scope, elem, attrs, ctrl) {
elem.addClass('bfh-currencies');
elem.addClass('bfh-selectbox');
elem.bfhselectbox({
filter: (elem.attr('data-filter') == 'true') ? true : false
}).on('change.bfhselectbox', function() {
return scope.$apply(function () {
return scope.ngModel = elem.val();
});
});
return elem.bfhcurrencies({
flags: (elem.attr('data-flags') == 'true') ? true : false,
currency: scope.ngModel || 'EUR'
});
}
};
});
这里的HTML片段
<div currencypicker id="cost-currency" data-flags="true" data-filter="true" ng-model="newEvent.cost_currency"></div>
问题是,当我尝试将我的模型值分配给具有scope.ngModel的currency属性时,它的评估结果总是未定义,因此分配了'EUR'值,我用firebug检查了我的作用域,其中ngModel值为"GBP"。我不明白为什么会发生这种事。
使用指令的方式如下:
app.directive('currencypicker', function () {
return {
restrict: 'A',
require: '?ngModel',
//template : you have no template nor url template?
link: function (scope, elem, attrs, ngModelCtrl) {
// You can have your "ngModelValue" into ngModelCtrl.$viewValue
// Also, you now have access to all the functions declared into ngModelController
}
};
});
如果你想了解有关该主题的更多信息,请毫不犹豫地查看文档:https://docs.angularjs.org/api/ng/type/ngModel.NgModelController
相关文章:
- 在MVVM视图模型中处理应用程序范围的元素
- Angularjs无法将单选按钮与嵌套范围内的模型绑定
- 连接angularjs中的两个范围模型
- mocking和测试指令,隔离范围包含ng模型
- NG模型对我来说是矫枉过正的.任何仅在单击按钮时更新范围的替代方案,而不是每次都更新范围
- 视图模型中的功能范围
- 范围不更新模型中的更改
- Angularjs 绑定 2 指令将模型范围限定为父控制器模型
- 杜兰达尔视图模型中的范围界定
- 重复离子范围输入的去冲突ng模型
- 主干.js - 模型方法中“this”的范围
- 在范围之外调用 AJAX 成功的淘汰模型函数
- 角度模型放宽了指令中包含ng的范围
- 主干模型/集合中的Javascript范围界定问题
- Angular指令无法确定ng模型的范围
- Angular-使用范围变量设置ng模型
- 模型窗口内的AngularJS范围
- 灰烬把手不会自动将模型放入范围
- Angular数据绑定没有更新正确的模型范围
- AngularJS:如何根据指令设置ng模型或更改范围值