Angular JS $ interpolate查询

时间:2014-11-08 15:40:23

标签: javascript angularjs

我是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”是做什么的?

1 个答案:

答案 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字符串并将其添加到标记

scope mapping

<强>参考