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

标签 dart angular-dart

我有两个相互嵌套的 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/

相关文章:

ubuntu - 使用 Ansible 安装 Dart 的包

dart - flutter ScrollController 附加到多个 ScrollView

dart - 在 Angular Dart 中更改 HTML 的 <head> 中的值

dart - 文档:单击不起作用

dart - 太多导入导致 `pub serve` 挂起

dart - Angular Dart表单中的细粒度错误消息

installation - 如何升级到 Dart 2?

dart - 为什么 FocusNode 需要在 flutter 中配置?

dart - flutter 打开/关闭wifi

angularjs - Angular Dart 中与观察者相关的令人惊讶的错误消息