提问人:Indrick 提问时间:7/21/2015 最后编辑:ngLoverIndrick 更新时间:7/21/2015 访问量:41
指令中的$compile
$compile in Directives
问:
嘿,伙计们,我正在计划我正在制定的指令,它本质上是一个带有计时器的弹出窗口。基本上,计划是传入一个对象,该对象可以配置属性以构造消息。该指令将包含 html 模板,我们将根据服务中设置的属性附加 message/html。例如:
$rootScope.timer = 'recursive time fn goes here'
obj = {
message : '<span ng-click="connect()">Custom message goes here {{ timer }} </span>'
}
Popup.pop(obj);
等。问题的重点是计时器需要向下滴答(这在控制器中很容易做到),但是如果插入,该指令会将 html 设置为字符串,如果我是正确的,则不会更新该值。我的问题是我如何获得指令以使计时器在指令中滴答作响。我需要在 ?如果是这样,如何?此外,如果我需要函数,我将如何从此服务传递函数?对不起,如果它令人困惑,请提出问题。$rootScope
$compile
directive
ng-click
答:
0赞
Phani Kumar Vadlamani
7/21/2015
#1
试试这个
//you can add your custom messge and time function returning value to the way u want
// this is the basic way to do
var testing = angular.module('testing', [])
testing.directive('mydir', function ($compile, $rootScope) {
var template = '<span ng-click="connect()">custom message</span>'
return {
restrict: 'E',
link: function (scope, ele, attribute) {
scope.connect = function () {
alert('popup' + new Date().getTime());
}
var content = $compile(template)(scope);
ele.append(content)
}
}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body>
<div ng-app="testing">
<mydir></mydir>
</div>
</body>
上一个:为两个视图创建指令
评论