ng-model inside ng-transclude

前端 未结 1 745
面向向阳花
面向向阳花 2021-01-23 05:31

I have an issue when using ng-model inside ng-transclude.

As ng-transclude creates child scopes the value can\'t be set to the out

1条回答
  •  隐瞒了意图╮
    2021-01-23 06:05

    as the $parent may refer to a different scope, depending on the context, it is advisable that you declare an object to hold properties you intend to write into (e.g. $scope.data = {text: "foo"}; ) , so that when the ng-model is trying to write the value (via ng-model="data.text"), it will have to make a "read" first, looking along the prototype chain, until it finally reaches the "data" property on the desired scope (assuming there is no other scope that has that property along the way).

    This approach follows the "always use the dot in ng-model" rule.

    (side note: another possible approach is to use an alias for the controller, assuming it is available in the angular version you are using).

    {{my.text}}

    http://plnkr.co/edit/aESrHtuSH9cd9ljyQAfH?p=preview

    0 讨论(0)
提交回复
热议问题