【发布时间】:2015-03-26 08:15:24
【问题描述】:
我一辈子都无法解决这个问题;我什至无法确定这是 AngularUI 路由器还是 Ionic 本身的问题。
我正在尝试使用自定义 AngularJS 指令完成双向绑定(即指令定义中的 scope: {data: "="}),它可以完美地工作,正如 this jsfiddle 所展示的那样,但在我的特定上下文中使用的完全相同的代码(即:在我使用“更改数据”按钮进入页面之前,我浏览了两种状态)没有(jsfiddle here)。
所以提示我添加一些代码以沿着 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
*/
【问题讨论】:
-
今天遇到了类似的问题,通过阅读解决了这个问题:sitepoint.com/understanding-angulars-apply-digest 不确定这是否是您需要的。
-
会调查的,谢谢。
标签: javascript angularjs angularjs-directive angular-ui-router ionic-framework