gpt4 book ai didi

dart - 角 Dart : Data binding doesn't work when manipulating the controller from the outside

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

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

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

Controller 外部还有一个“打开”链接。其onclick处理程序调用外部 Controller 并应该通过模型检查复选框。问题是即使模型发生了变化, View 也没有得到更新。

这种模式在 AngularJS 中完美运行,但在 AngularDart 中显然不行。我假设 Dart 区域是罪魁祸首(目前我对此一无所知)。

我坚持使用这种模式或类似的模式,因为我正在将 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.shouldShowInnerController">
<input type="checkbox" ng-model="outerCtrl.shouldShowInnerController">
<div inner-controller ng-switch-when="true">
Your name: <input ng-model="innerCtrl.yourName">
<br>
Hello {{innerCtrl.yourName}}!
</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 shouldShowInnerController;
static Scope scope;
OuterController(Scope _scope) {
scope = _scope;
outerController = this;
}

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

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

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>

最佳答案

适用于 Dart 1.5.1 和 AngularDart 0.12.0

我只初始化了 shouldShowInnerController bool 值

<!DOCTYPE html>

<html ng-app>
<head>
<title>Angular.dart nested controllers</title>
</head>
<body>
<a href="#" id="open">open</a>
<div outer-controller ng-switch="outerCtrl.shouldShowInnerController">
<input type="checkbox" ng-model="outerCtrl.shouldShowInnerController">
<div inner-controller ng-switch-when="true">
Your name: <input ng-model="innerCtrl.yourName">
<br>
Hello {{innerCtrl.yourName}}!
</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 shouldShowInnerController = false;
static Scope scope;
OuterController(Scope _scope) {
scope = _scope;
outerController = this;
}

void showOuterController() {
shouldShowInnerController = !shouldShowInnerController;
scope.apply();
}
}

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

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>

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

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