javascript - 范围更改不执行指令中的代码

标签 javascript angularjs angularjs-directive angularjs-scope

这是我的指令的样子

//noinspection JSCheckFunctionSignatures
angular.module('notificationDirective', []).directive('notification', function ($timeout) {
    //noinspection JSUnusedLocalSymbols
    return {
        restrict: 'E',
        replace: true,
        scope: {
            ngModel: '@'
        },
        template: '<div class="alert fade" bs-alert="ngModel"></div>',
        link: function (scope, element, attrs) {
            scope.$watch('message', function () {
                console.log('message now: ' + JSON.stringify(scope.message));
//                element.show();
//                //noinspection JSUnresolvedFunction
//                $timeout(function () {
//                    //element.empty();
//                    element.fadeOut("slow");
//                }, 2000);
            });
        }
    }
});

这是我的 Controller

function NotificationController($scope) {
    $scope.message = {};
    console.log('notification activated');

    $scope.invite = function() {
        console.log("submitting invite for " + $scope.email);
        $scope.message.title = 'hello';
//        console.log('message now is ' + JSON.stringify($scope.message, null, 2));
    }
}

我是这样用的

<form class="pure-form" data-ng-controller="NotificationController">
    <input type="text" class="pure-input-1-2" placeholder="Email"
           data-ng-model="email">
    <button type="submit"
            class="pure-button notice pure-button-xlarge"
            data-ng-click="invite()">Invite me
    </button>
</form>
<notification data-ng-model="message"></notification>

我想要的
- 每当 NotificationController 中的 scope.message 值发生变化时,指令就会有新值,这样我就可以在网页上提醒它

我不明白的地方
看来我不明白 $scope.$watch 是如何工作的

请帮忙

最佳答案

你犯了几个错误:

  1. 您的通知标记必须位于 Controller 内(在 html 中),因为它必须能够访问“消息”变量。
  2. 您在指令中的绑定(bind)是错误的:您必须使用“=”而不是“@”(如 Thalis 所说)。
  3. 变量“message”在您的指令中不存在,您必须使用 scope.ngModel(绑定(bind)到您的 message 变量)。
  4. 每次更新变量时,都会执行观察者中的回调。由于您使用了一个对象,因此当您的变量引用发生变化时,观察者将被执行。您必须将“true”设置为观察者的第三个参数以检查对象是否相等)。

这是您的示例:

HTML:

<body>
    <div id="my-app" data-ng-app="myApp">
        <form class="pure-form" data-ng-controller="NotificationController">
            <input type="text" class="pure-input-1-2" placeholder="Email" data-ng-model="email" />
            <button type="submit"
              class="pure-button notice pure-button-xlarge"
              data-ng-click="invite()">Invite me
            </button>
            <notification data-ng-model="message"></notification>
        </form>        
    </div>
</body>

Javascript:

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

myApp.directive('notification', function() {
    return {
        restrict: 'E',
        replace: true,
        scope: {
            ngModel: '='
        },
        template: '<div class="alert fade" bs-alert="ngModel"></div>',
        link: function (scope, element, attrs) {
            scope.$watch('ngModel', function () {
                console.log('message now: ' + JSON.stringify(scope.ngModel));
//                element.show();
//                //noinspection JSUnresolvedFunction
//                $timeout(function () {
//                    //element.empty();
//                    element.fadeOut("slow");
//                }, 2000);
            }, true);
        }
    }
});

myApp.controller('NotificationController', ['$scope', function($scope) {
    $scope.message = {};
    console.log('notification activated');

    $scope.invite = function() {
        console.log("submitting invite for " + $scope.email);
        $scope.message.title = 'hello';
//      console.log('message now is ' + JSON.stringify($scope.message, null, 2));
    };
}]);

请参阅此 fiddle :http://jsfiddle.net/77Uca/15/

关于javascript - 范围更改不执行指令中的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18445401/

相关文章:

Javascript在for循环中每5次添加一次延迟

angularjs - 定义 Angular 应用程序有什么好处?

angularjs - ng-repeat 和 ng-switch 不适用于同一元素

javascript - 如何在单击按钮时在angularjs中动态添加输入字段?

javascript - 使用 Javascript 将图像序列转换为视频

javascript - angularjs( ionic )中的表单验证对我不起作用

javascript - 即使存在 CORS header ,AJAX 请求也会失败

javascript - AngularJS观察div大小

javascript - AngularJS:按属性名称过滤对象数组

angularjs - ui.bootstrap.datepicker是打开的,无法在模式下工作