【问题标题】:Angular Dart: Data binding doesn't work when manipulating the controller from the outsideAngular Dart:从外部操作控制器时数据绑定不起作用
【发布时间】:2014-06-26 20:10:26
【问题描述】:

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

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

还有一个“打开”链接在控制器之外。它的onclick 处理程序调用外部控制器并应该通过模型检查复选框。问题是即使模型改变了,视图也没有更新。

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

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

依靠您的终极智慧,在此先感谢!

<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 angular-dart


    【解决方案1】:

    适用于 Dart 1.5.1 和 AngularDart 0.12.0

    我只初始化了 shouldShowInnerController 布尔值

    <!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>
    

    【讨论】:

    • 你是今天的英雄!它的工作原理真是太神奇了。我假设将成员初始化为 null 不应该导致这种行为,还是应该这样做?谢谢!
    • 我刚刚发布了问题的第二部分,以防您感兴趣。谢谢! stackoverflow.com/questions/24677379/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-19
    • 1970-01-01
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    • 2018-12-11
    • 2019-04-09
    相关资源
    最近更新 更多