gpt4 book ai didi

javascript - 将 ngModel 传递给 AngularJS 组件

转载 作者:行者123 更新时间:2023-11-30 15:39:02 28 4
gpt4 key购买 nike

我正在尝试创建一个自定义表单组件,我可以使用 ng-model 与之绑定(bind)。我找到了很多关于如何使用指令执行此操作的教程,但我似乎无法弄清楚如何使用组件来执行此操作,因为您不能对组件使用 link 函数,并尝试将 ngModel 依赖项传递给 Controller ​​会导致此错误:

Error: [$injector:unpr] Unknown provider: $ngModelProvider <- $ngModel

我也看过 this并试过这个:

bindings: {
value: "=ngModel"
}

我试过了,然后在 Controller 中为双向绑定(bind)设置值,但它似乎不起作用。

最佳答案

这是对 Angular 中组件的常见误解。组件不应修改其自身范围之外的数据。所以,如果你想这样做,你真的应该使用指令。

这是来自 documentation 的引述:

Components only control their own View and Data: Components should never modify any data or DOM that is out of their own scope. Normally, in Angular it is possible to modify data anywhere in the application through scope inheritance and watches. This is practical, but can also lead to problems when it is not clear which part of the application is responsible for modifying the data. That is why component directives use an isolate scope, so a whole class of scope manipulation is not possible.

关于javascript - 将 ngModel 传递给 AngularJS 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41093499/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com