這裏是我的指令是什麼樣子範圍變更不執行指令代碼
//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