这里是我的指令是什么样子范围变更不执行指令代码
//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);
});
}
}
});
这里是我的控制器
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>
我想要什么
- 当值,如果scope.message
变化NotificationController
,该指令有新的价值,这样我可以提醒网页上
我不明白
好像我不理解怎么$scope.$watch
工作
请帮助
这是有益的,但它不工作每当我更改文本框中的值时,它都可以正常工作一旦。尝试多次尝试,你会发现它不起作用 – daydreamer
我的不好,它正在工作。感谢您的详细回复 – daydreamer
它不起作用,因为您的变量永远不会改变,请尝试设置输入值而不是“hello”:) – Mickael