在 ng-if 中访问 $scope [英] get access to $scope in ng-if

查看:28
本文介绍了在 ng-if 中访问 $scope的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的指令中访问由 ng-if 创建的范围的更好方法是什么(没有 $parent.params.text):

What is the better approach to get access to scope created by ng-if in my directive (without $parent.params.text):

<span ng-if="params" uib-tooltip="{{params.text}}"></span>

.directive('myDirective', functions(){
   return {
      templateUrl: 'template.html',
      replace: true,
      $scope: {
         data: '='
      },
      controller: function(){
          if (data) { //some logic
             $scope.params.text = 'text'
          }
      }
   }
})

推荐答案

我注意到如果我的变量嵌套在对象中,我不必使用 $parent.

I've noticed that I don't have to use $parent if my variable is nested inside an object.

例如:

控制器$scope.params = { ... }

查看 ng-if="params"

行不通,但是:

控制器

$scope.something_here = {};
$scope.something_here.params = { ... }

查看 ng-if="something_here.params"

工作.如果您尝试访问的键是对象的一部分,我相信 Angular 会保留范围.试试看!

would work. I believe Angular preserves the scope if the key you're trying to access is part of an object. Give it a try!

这篇关于在 ng-if 中访问 $scope的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆