小编Ste*_*eve的帖子

AngularJS:父范围未在指令(具有隔离范围)双向绑定中更新

我有以下代码,也可以在http://jsfiddle.net/garukun/u69PT/上摆弄.

视图:

<div data-ng-app="testApp">
    <div data-ng-controller="testCtrl">
        <strong>{{pkey}}</strong>
        <span data-test-directive data-parent-item="pkey" 
            data-parent-update="update(pkey)"></span>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

JS:

var testApp = angular.module('testApp', []);

testApp.directive('testDirective', function ($timeout) {
    return {
        scope: {
            key: '=parentItem',
            parentUpdate: '&'
        },
        replace: true,
        template: '<div><p>{{key}}</p>' +
            '<button data-ng-click="lock()">Lock</button>' +
            '</div>',
        controller: function ($scope, $element, $attrs) {
            $scope.lock = function () {
                $scope.key = 'D+' + $scope.key;
                console.log('DIR :', $scope.key);

                // Expecting $scope.$parent.pkey to have also been
                // updated before invoking the next line.
                $scope.parentUpdate();
                // $timeout($scope.parentUpdate); // …
Run Code Online (Sandbox Code Playgroud)

javascript scope directive two-way angularjs

10
推荐指数
1
解决办法
2万
查看次数

标签 统计

angularjs ×1

directive ×1

javascript ×1

scope ×1

two-way ×1