我在jsFiddle中有相同的指令示例,在plnkr中有另一个。在jsFiddle中,我必须单击文本两次才能更改文本,而在plnkr中,它的工作方式与预期的一样。有人能指出这是一种预期的行为还是只是一个bug吗。
http://jsfiddle.net/z6bfC/
http://plnkr.co/edit/2Z1u2PzbRzPYzjPv4miG?p=preview
HTML:
<div ng-app="testModule">
<div ng-controller="MyCtrl">
<div>{{test}}
<br/>
<test-directive class="width" bindme="test" style="color: red;">Click Me Twice!!</test-directive>
</div>
</div>
</div>
指令/控制器:
angular.module('testModule', []).directive('testDirective', function () {
return {
restrict: 'E',
scope: {
bindme: "="
},
link: function (scope, element, attr) {
element.bind('click', function () {
scope.bindme = "Changed";
if (!scope.$$phase) {
console.log(scope.bindme);
scope.$apply();
} else {
console.log("Something");
}
});
}
};
}).
controller('MyCtrl', function ($scope) {
$scope.test = "Some string";
});
谢谢!
AngularJS 1.1.0似乎做了一些时髦的事情。
试试1.1.1。http://code.angularjs.org/1.1.1/angular.min.js