我是Angular JS的新手,并试图理解指令功能,如下所示
function mydirective($interpolate, $compile) {
return {
restrict: 'E',
scope: {
mycontent: '=',
myurls: '=',
mydata: '@'
},
replace: true,
template: '<div ng-bind-html="html"></div>',
link: function ($scope, element, attrs) {
$scope.$watch('mycontent', function (value) {
var html = $interpolate(value)($scope);
element.html(html);
$compile(element.contents())($scope);
});
}
}
}
我无法从上面理解。
1)这个$ interpolate(值)($ scope)有什么作用?什么是第二个参数$ scope。
2)这个$ compile函数在做什么?
3)模板中的div ng-bind-html =“html”是做什么的?
答案 0 :(得分:0)
这个$ interpolate(值)($ scope)做什么?
获取value
变量
这是第二个参数$ scope。
它提供了数据绑定的上下文
it('should interpolate with undefined context', inject(
function($interpolate)
{
expect($interpolate("Hello, world!{{bloop}}")()).toBe("Hello, world!")
}
));
这个$ compile函数在做什么?
它将字符串转换为Angular标记
模板中的div ng-bind-html =“html”它做了什么?
它编译$scope.html
中的HTML字符串并将其添加到标记
<强>参考强>