当前位置: 动力学知识库 > 问答 > 编程问答 >

angularjs - What is Diff between scope and Isolate scope

问题描述:

While implementing directive I came across isolate scope , I am having Confusion why we have to Use isolate scope instead of scope.

网友答案:

When using an isolated scope, the directive's scope does not prototypically inherit from its parent. The directive has no access to the parent scope. This gives you the highest encapsulation. You should use an isolated scope, whenever you're designing reusable components.

网友答案:

Directives have access to the parent scope by default. For example, the following directive relies on the parent scope to write out a user object’s name:

 angular.module('myDirective').directive('sharedScope', function () {
    return {
        template: 'Name: {{user.name}}'
    };
});

The problem with this code is that you need to have an information about the parent scope, thus if the parent scope changes? The directive will become not usable anymore. That's when isolated scope comes in handy. Therefore isolated scope is used whenever directive is designed to be reusable. These are some good blog post which explain in depth the topic

  • AngularJS Directives, Using Isolated Scope with Attributes
  • Angularjs Sticky Notes
  • Creating Custom AngularJS directives
分享给朋友:
您可能感兴趣的文章:
随机阅读: