AngularJS指令双向绑定在Ionic中不起作用



我一辈子都无法解决这个问题;我什至无法弄清楚这是AngularUI路由器还是Ionic本身的问题。

我正在尝试完成双向绑定(即 scope: {data: "="}在指令定义中)使用自定义 AngularJS 指令,它完美地工作,如这个 jsfiddle 所示,但在我的特定上下文中使用的相同确切代码(即:我在进入带有"更改数据"按钮的页面之前浏览两种状态)没有(jsfiddle 在这里)。

SO 提示我添加一些代码以沿着 jsfiddle 链接进行,所以这里是工作版本。

页:

<!-- HTML -->
<body ng-app="myApp">
    <div ng-controller="MyCtrl">
        <signature data="signatureData"></signature>
        <button ng-click="changeData()">Change data!</button>
    </div>
</body>

脚本:

//JS
angular.module("myApp", [])
.controller("MyCtrl", ["$scope", function ($scope) {
    $scope.changeData = function() {
        console.log("Outside directive:", $scope.signatureData);
        $scope.signatureData = "hello";
    };
}])
.directive("signature", [function() {
    var directiveDefinitionObject = {
        template: "<div>Don't mind me, I'm just a template...</div>",
        scope: {
            data: "="
        },
        controller: ["$scope", "$element", function($scope, $element) {
            $scope.data = "ciao";
            $scope.$watch("data", function(d) {
                console.log("Inside directive:", d);
            });
        }]
    };
    return directiveDefinitionObject;
}]);
/* Console output when clicking button:
Inside directive: ciao
Outside directive: ciao
Inside directive: hello
*/

相同的代码,虽然放在上下文中,但在第二个小提琴中,它太冗长了,无法粘贴在这里(我为此道歉,我已经尽可能多地浏览了它,但这只是为了了解问题以防万一它有帮助)。

但是,控制台输出为:

/* Console output when clicking button:
Inside directive: ciao
Outside directive: undefined
*/

$scope.signatureData 在调用 $scope.signatureData = "hello" 之前没有定义

;

尝试定义签名数据,当您转到该控制器时...

.controller("JobRegistrationCtrl", ["$scope", function ($scope) {
    $scope.signatureData = "ciao"; //Now it's defined
    $scope.changeData = function() {
        console.log("Outside directive:", $scope.signatureData);
        $scope.signatureData = "hello";
    };
}])
我想

我已经指出了这个问题。

显然,AngularUI Router(j'accuse!)是负责任的,因为它用它的范围做了一些奇怪的笨拙。我已经更新了两个小提琴:工作示例,破碎的示例。如您所见,我在两个小提琴中添加了相关作用域 id 的日志:工作示例正确地指出,指令是双向绑定其作用域 3 中的模型与其父作用域(作用域 2)中的模型,并且应该看到这些更改的控制器确实是数字 2。另一方面,破碎的示例强调了手头的问题:指令(范围 24)将其模型绑定到父范围 (23),但应该反映更改的控制器是编号 22,因此不匹配。

angular.module("myApp", [])
.controller("MyCtrl", ["$scope", function ($scope) {
    console.log("Parent's scope id is:", $scope.$id);
    $scope.changeData = function() {
        console.log("Outside directive:", $scope.signatureData);
        $scope.signatureData = "hello";
    };
}])
.directive("signature", [function() {
    var directiveDefinitionObject = {
        template: "<div>Don't mind me, I'm just a template...</div>",
        scope: {
            data: "="
        },
        controller: ["$scope", "$element", function($scope, $element) {
            console.log("Two-way binding between directive's scope (%d) and directive's parent scope (%d)", $scope.$id, $scope.$parent.$id);
            $scope.data = "ciao";
            $scope.$watch("data", function(d) {
                console.log("Inside directive:", d);
            });
        }]
    };
    return directiveDefinitionObject;
}]);

结果(控制台日志):

// Working version:
/*
Parent's scope id is: 2
Two-way binding between directive's scope (3) and directive's parent scope (2)
Inside directive: ciao
Outside directive: ciao
Inside directive: hello
*/
//Broken version
/*
Parent's scope id is: 22
Two-way binding between directive's scope (24) and directive's parent scope (23)
Inside directive: ciao
Outside directive: undefined
*/

现在我希望一些AngularUI路由器专家能够加入并挽救一天。

在此之后

您必须使用"虚线"表示法

最新更新