在AngularJS中共享控制器和指令之间的范围

sim*_*aun 8 angularjs angularjs-directive angularjs-scope

我已经创建了一个包装jQuery插件的指令,并将插件的配置对象从控制器传递给指令.(作品)

在config对象中是我想要在事件上调用的回调.(作品)

在回调中,我想修改控制器的$ scope上的属性,这不起作用.Angular不承认该属性由于某种原因而发生了变化,这使我相信回调中的$ scope与控制器的$ scope不同.我的问题是我不知道为什么.

任何人都能指出我正确的方向吗?


点击这里查看小提琴


app.js

var app = angular.module('app', [])
    .directive('datepicker', function () {
        return {
            restrict: 'A',
            link: function (scope, element, attrs) {
                // Uncommenting the line below causes
                // the "date changed!" text to appear,
                // as I expect it would.
                // scope.dateChanged = true;

                var dateInput = angular.element('.datepicker')

                dateInput.datepicker(scope.datepickerOpts);

                // The datepicker fires a changeDate event
                // when a date is chosen. I want to execute the
                // callback defined in a controller.
                // ---
                // PROBLEM: 
                // Angular does not recognize that $scope.dateChanged
                // is changed in the callback. The view does not update.
                dateInput.bind('changeDate', scope.onDateChange);
            }
        };
    });

var myModule = angular.module('myModule', ['app'])
    .controller('MyCtrl', ['$scope', function ($scope) {
        $scope.dateChanged = false;

        $scope.datepickerOpts = {
            autoclose: true,
            format: 'mm-dd-yyyy'
        };

        $scope.onDateChange = function () {
            alert('onDateChange called!');

            // ------------------
            // PROBLEM AREA:
            // This doesnt cause the "date changed!" text to show.
            // ------------------
            $scope.dateChanged = true;

            setTimeout(function () {
                $scope.dateChanged = false;
            }, 5000);
        };
    }]);
Run Code Online (Sandbox Code Playgroud)

HTML

<div ng-controller="MyCtrl">
    <p ng-show="dateChanged">date changed!</p>
    <input type="text" value="02-16-2012" class="datepicker" datepicker="">
</div>
Run Code Online (Sandbox Code Playgroud)

cha*_*tfl 11

您的演示中有许多范围问题.首先,在dateChange回调中,即使函数本身在控制器this内声明,回调内的上下文也是bootstrap元素,因为它在引导程序处理程序中.

每当您从第三方代码中更改角度范围值时,角度需要通过使用来了解它$apply.通常最好将所有第三方范围保留在指令中.

一个更有棱角的apprroach是ng-model在输入上使用.然后$.watch用于更改模型.这有助于将控制器内的所有代码保持在角度上下文中.在任何角度应用中都很少见,不能ng-model在任何表格控件上使用

 <input type="text"  class="datepicker" datepicker="" ng-model="myDate">
Run Code Online (Sandbox Code Playgroud)

在指令内:

dateInput.bind('changeDate',function(){
      scope.$apply(function(){
         scope[attrs.ngModel] = element.val()
      });
});
Run Code Online (Sandbox Code Playgroud)

然后在控制器中:

$scope.$watch('myDate',function(oldVal,newVal){ 
       if(oldVal !=newVal){
           /* since this code is in angular context will work for the hide/show now*/
            $scope.dateChanged=true;
             $timeout(function(){
                   $scope.dateChanged=false;
              },5000);
        }        
});
Run Code Online (Sandbox Code Playgroud)

演示:http://jsfiddle.net/qxjck/10/

编辑var dateInput = angular.element('.datepicker')如果要在页面中的多个元素上使用此指令,则应删除另一个项目.在指令中使用它是多余的,其中已经是回调element中的一个参数link,并且是特定于实例的.替换dateInputelement