gpt4 book ai didi

dart - Angular Dart : Data binding doesn't work when manipulating the controller from the outside, 第二部分

转载 作者:行者123 更新时间:2023-12-03 03:46:27 24 4
gpt4 key购买 nike

简短背景:

这个例子是我的 Angular Dart: Data binding doesn't work when manipulating the controller from the outside 的一个稍微复杂的版本。已正确回答的问题。我只在这个版本中添加了一个可切换的“显示已解决的评论”链接。即使我将每个变量都初始化为非空值,问题仍然存在。

实际问题的完整描述:

我有两个相互嵌套的 Controller 。外部 Controller 使用 ng-switch 指令显示/隐藏内部 Controller 。

外部 Controller 还包含一个复选框。如果选中此复选框,则内部 Controller 可见(通过上述 ng-switch 指令)。此复选框按预期工作。

Controller 外部还有一个“打开”链接。它的 onclick 处理程序调用外部 Controller ,并应该通过模型检查复选框。问题是即使模型改变了, View 也不会更新,除非我明确调用 scope.apply() ,我不应该这样做。即使我删除了 scope.apply() 之前的评论在我的代码中,数据绑定(bind)在 InnerController 中不起作用。

这种模式在 AngularJS 中完美运行,但在 AngularDart 中显然不行。

我坚持使用这种模式或类似的模式,因为我正在将 AngularDart 集成到不使用数据绑定(bind)的遗留应用程序中,因此我必须从模型外部触发模型更改。

提前致谢!

<html ng-app>
<head>
<title>Angular.dart nested controllers</title>
</head>
<body>
<a href="#" id="open">open</a>
<div outer-controller ng-switch="outerCtrl.showInnerController">
<input type="checkbox" ng-model="outerCtrl.showInnerController">
<div inner-controller ng-switch-when="true">
Your name: <input ng-model="innerCtrl.yourName">
<br>
Hello {{innerCtrl.yourName}}!
<div ng-switch="innerCtrl.showResolvedComments" style="text-decoration:underline; color:blue; cursor:pointer">
<div ng-switch-when="true" ng-click="innerCtrl.showResolvedComments = false">Hide resolved comments</div>
<div ng-switch-when="false" ng-click="innerCtrl.showResolvedComments = true">Show resolved comments</div>
</div>
</div>
<div inner-controller ng-switch-when="false">
other controller
</div>
</div>
<script type="application/dart">
import "dart:html";
import 'package:angular/angular.dart';
import 'package:angular/application_factory.dart';

OuterController outerController;
@Controller(selector:'[outer-controller]', publishAs:'outerCtrl')
class OuterController {
bool showInnerController = false;
Scope scope;
OuterController(this.scope) {
outerController = this;
}

void showOuterController() {
showInnerController = true;
//scope.apply();
}
}

@Controller(selector:'[inner-controller]', publishAs:'innerCtrl')
class InnerController {
String yourName = 'defaultName';
bool showResolvedComments = true;
}

class MyAppModule extends Module {
MyAppModule() {
type(InnerController);
type(OuterController);
}
}

main() {
applicationFactory().addModule(new MyAppModule()).run();
querySelector('#open').onClick.listen((Event event) {
outerController.showOuterController();
});
}
</script>
</body>
</html>

最佳答案

经过一些实验,激活 ng-model 看起来像是 Angular 监听指定事件,而且看起来并不是每个变量都发生变化,我认为因为在不影响性能的情况下观察变量的每个变化是很复杂的。

您可以通过模拟用户单击复选框来更改方法
像:

 CheckboxInputElement checkBox = querySelector("input");
if (checkBox.checked == false) {
checkBox.click();
}

这可能不是更清洁的方法,但它有效

这是带有补丁的完整代码

<html ng-app>
<head>
<title>Angular.dart nested controllers</title>
</head>
<body>
<a href="#" id="open">open</a>
<div outer-controller ng-switch="outerCtrl.showInnerController">
<input type="checkbox" ng-model="outerCtrl.showInnerController">
<div inner-controller ng-switch-when="true">
Your name: <input ng-model="innerCtrl.yourName">
<br>
Hello {{innerCtrl.yourName}}!
<div ng-switch="innerCtrl.showResolvedComments" style="text-decoration:underline; color:blue; cursor:pointer">
<div ng-switch-when="true" ng-click="innerCtrl.showResolvedComments = false">Hide resolved comments</div>
<div ng-switch-when="false" ng-click="innerCtrl.showResolvedComments = true">Show resolved comments</div>
</div>
</div>
<div inner-controller ng-switch-when="false">
other controller
</div>
</div>
<script type="application/dart">
import "dart:html";
import 'package:angular/angular.dart';
import 'package:angular/application_factory.dart';

OuterController outerController;
@Controller(selector:'[outer-controller]', publishAs:'outerCtrl')
class OuterController {
bool showInnerController = false;
Scope scope;
OuterController(this.scope) {
outerController = this;
}

void showOuterController() {
showInnerController = true;
print("showOuterController");
//scope.apply();
}
}

@Controller(selector:'[inner-controller]', publishAs:'innerCtrl')
class InnerController {
String yourName = 'defaultName';
bool showResolvedComments = true;
}

class MyAppModule extends Module {
MyAppModule() {
type(InnerController);
type(OuterController);
}
}

main() {
applicationFactory().addModule(new MyAppModule()).run();
querySelector('#open').onClick.listen((Event event) {
outerController.showOuterController();
// Added Code
CheckboxInputElement checkBox = querySelector("input");
if (checkBox.checked == false) {
checkBox.click();
}
// End added code
});
}
</script>
</body>
</html>

关于dart - Angular Dart : Data binding doesn't work when manipulating the controller from the outside, 第二部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24677379/

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