2016-06-21 69 views
0

我是Angularjs的新手。有人可以帮助找到这个问题吗?双重绑定在这个例子中不起作用。双向绑定在此示例中不起作用

http://jsfiddle.net/gM2Hg/120/

<body ng-app="animateApp"> 
    <br/> the watch is triggered only on the load of the page, not on subsequent changes to the property "acts" 
    <div ng-controller="tst"> 
    <input type="button" bn="" acts="acts" value="Add Action" /> {{acts.crop}} 
    </div> 
</body> 

var animateAppModule = angular.module('animateApp', []) 

animateAppModule.controller('tst', function($scope) { 
    $scope.acts = { 
    crop: "test" 
    }; 
    $scope.$watchCollection('model.acts', function(newValue, oldValue) { 
    alert("as"); 
    }) 
}) 

animateAppModule.directive('bn', function() { 
    return { 
    restrict: "A", 
    scope: { 
     acts: '=' 
    }, 
    link: function($scope, iElement, iAttrs) { 
     alert("l"); 
     iElement.click(function() { 
     alert($scope.acts.crop); 
     $scope.acts = { 
      crop: "real" 
     }; 
     alert($scope.acts.crop); 
     }) 
    } 
    } 
}) 
+0

您是否收到错误讯息?做事情呈现? – Makoto

回答

2

首先你要编写这样的手表功能:

$scope.$watch('acts', function(newValue, oldValue) { 
    alert("as"); 
}); 

现在你已经设置你的程序指令更改范围后申请:

$scope.$apply(); 

检查更新jsFiddle:http://jsfiddle.net/gM2Hg/121/

希望这会对你有所帮助。谢谢。

+0

谢谢。这就是诀窍! $范围$适用()。为什么我们需要明确调用这个函数? – dotnet

+0

是的,它会强制更新你的范围。 –

相关问题