我的HTML看起來像這樣顯示:數據沒有創建指令
<body ng-app="customDirective">
<div ng-controller="anController">
Date format: <input ng-model="fooData"> <hr/>
Current time is: <my-new-directive foo-Attr="{{fooData}}"></my-new-directive>
</div>
</body>
</html>
我想如果scope屬性scope.fooData
是改變了我創建標籤my-new-directive
內更新數據。
所以我有一個作用域屬性稱爲fooData
:
$scope.fooData = 'Hello World!:)';
和我創建一個指令:
(function(angular) {
'use strict';
angular.module('customDirective', [])
.controller('anController', ['$scope', function($scope) {
$scope.fooData = 'Hello World!:)';
}])
.directive('myNewDirective', [function() {
function link(scope, element, attrs) {
var format,
timeoutId;
function updateValue() {
element.text(format);
}
scope.$watch(scope.fooData, function(value) {
format = value;
updateValue();
});
}
return {
restrict: 'A',
link: link,
scope: {
fooAttr: '@'
}
};
}]);
document.createElement('my-new-directive');
})(window.angular);
但如果我寫的輸入標籤裏面新的值,則沒有在發生我新指令。
任何幫助將不勝感激!我正在使用AngularJS 1.5.8。
非常感謝!你是我的英雄!非常感謝! – StepUp
謝謝!它完美的作品!但你爲什麼認爲這個指令過於複雜? – StepUp