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

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

问题描述

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

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天全站免登陆