Home  >  Q&A  >  body text

angular.js - angularjs中自定义指令scope的问题

angularjs中自定义的指令中scope的值为{}创造了一个隔离作用域,但依然能访问到父级作用域中的属性,怎么回事?(隔离作用域应该不能访问父级作用域的属性啊,在没有进行绑定策略之前)

淡淡烟草味淡淡烟草味2733 days ago773

reply all(4)I'll reply

  • 巴扎黑

    巴扎黑2017-05-15 17:09:33

    First of all, you didn’t assign a value to the scope attribute in your example. The scope attribute has three values: true, false, {}. false is the default value.

    Default, false. At this time, the child scope and the parent scope are one, and the value of scope.$$id is the same.

    When

    is true, a new scope is created, and the scope.$$id value is different. However, this scope inherits from the parent scope. It is the same as the characteristics of the native js scope chain. The child scope can access the properties and methods in the parent scope, but the parent scope cannot access the properties and methods of the child scope.

    When

    is {}, it is real isolation. At this time, the scope of the custom directive has nothing to do with the parent scope. But what if you still want to access the property methods of the parent scope? Just configure the corresponding properties in {}. Please refer to Google Baidu for the specific configuration method.

    reply
    0
  • 天蓬老师

    天蓬老师2017-05-15 17:09:33

    You can access it in the same controller, but you can’t access it in different controllers

    reply
    0
  • ringa_lee

    ringa_lee2017-05-15 17:09:33

    You are not in quarantine. If you are in quarantine, add scope:{}

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-05-15 17:09:33

    There are two ways to isolate a scope:

    scope: true
    scope: {}

    reply
    0
  • Cancelreply