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

AngularJS directive two-way binding not working in Ionic

我一辈子都无法解决这个问题;我什至不知道这是 AngularUI Router 还是 Ionic 本身的问题。

我正在尝试使用自定义 AngularJS 指令完成双向绑定(即指令定义中的 scope: {data: "="}),它完美地工作,如 this jsfiddle but the same exact code used in my particular context (i.e.: I navigate through two states before I get to the page with the "Change data" button) doesn't (jsfiddle here 所示)。

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

相同的代码,虽然放在上下文中,但在第二个 fiddle 中过于冗长,无法粘贴到这里(对此我深表歉意,我已经尽可能地略读了它,但这只是为了提供一个理解以防有帮助)。

然而,控制台输出是:

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

$scope.signatureData 在您调用 $scope.signatureData = "hello";

之前未定义

尝试在转到该控制器时定义 signatureData...

.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 路由器 (j'accuse!) 负责,因为它的作用域做了一些奇怪的胡言乱语。我已经更新了两个小提琴:working example, broken example。如您所见,我在两个小提琴中都添加了所涉及范围 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 Router 专家能加入进来并节省时间。

正在关注 This

您必须使用 "dotted" 表示法