角度飞镖:当从外部操纵控制器时,数据绑定不工作,第二部分 [英] Angular Dart: Data binding doesn't work when manipulating the controller from the outside, part two

查看:183
本文介绍了角度飞镖:当从外部操纵控制器时,数据绑定不工作,第二部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简短背景

此示例是我的 Angular Dart:当从外部操作控制器时,数据绑定不工作已正确回答的问题。我只添加了一个可切换的显示已解决的评论链接到此版本。即使我将每个变量初始化为非空值,问题仍然会发生。

This example is a slightly more complicated version of my Angular Dart: Data binding doesn't work when manipulating the controller from the outside question that has been answered correctly. I only added a toggleable "show resolved comments" link to this version. Even though I initialized every variable to non-null values the problem still happens.

实际问题的完整说明

我有两个控制器嵌套在一起。外部控制器通过使用ng-switch指令显示/隐藏内部控制器。

I have two controllers nested into each other. The outer controller shows/hides the inner controller by using an ng-switch directive.

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

The outer controller also contains a checkbox. If this checkbox gets checked then the inner controller is made visible (via the above ng-switch directive). This checkbox works as intended.

控制器外部还有一个打开链接。它的onclick处理程序调用到外部控制器,并且应该通过模型检查复选框。问题是,即使模型更改,视图不会更新,除非我显式调用 scope.apply(),我不应该。即使我在我的代码中删除 scope.apply()之前的注释,那么数据绑定在InnerController中不起作用。

There's also an "open" link outside the controllers. Its onclick handler calls into the outer controller and is supposed to check the checkbox via the model. The problem is that even though the model gets changed, the view doesn't get updated, unless I explicitly call scope.apply(), which I shouldn't. Even if I remove the comment before scope.apply() in my code then data binding doesn't work within InnerController.

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

This pattern has worked flawlessly in AngularJS but apparently doesn't in AngularDart.

我坚持这个模式或类似的东西,因为我正在集成AngularDart进入不使用数据绑定的旧应用程序,因此我必须从模型外部触发模型更改。

I insist to this pattern or something similar because I'm in the process of integrating AngularDart into a legacy application that doesn't use data binding so I have to trigger model changes from outside the models.

提前感谢!

<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,它不会看到每个变量的变化,我认为因为观察变量的每个变化没有影响性能是复杂的。

After some experimentation, it's look like angular listen specified event to activate ng-model, and it doesn't look every variable change, i think because it's complicated to watch every change in variable without impact performance.

方法是通过模拟用户单击复选框
like:

You can change your approach by simulate a user click on the check box like:

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

这可能不是更干净的方法,但它工作

It's maybe not the cleaner way to do this, but it works

这里是补丁的完整代码

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

这篇关于角度飞镖:当从外部操纵控制器时,数据绑定不工作,第二部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆