坐落在指令$范围VAR在NG-点击 [英] Set $scope var in directive in ng-click

查看:142
本文介绍了坐落在指令$范围VAR在NG-点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的元素:

<img data-address ng-click="dishInfo = dish">

和指令:

app.directive('address', [
    function () {
        return {
            restrict: 'A',
            link: function($scope, element, attrs) {
                element.bind('click', function() {
                        console.log($scope.dishInfo);
                        $(".modal").modal();  
                })
            }
        }
    }
])

当我在控制台中点击图片我得到资源
  {资源ID = 1,菜= {...},类别= [1],...}
在我的 .modal 这code:

When i click on image in my console i got Resource: Resource { id=1, dishes={...}, category=[1],...} in my .modal this code:

<div class="partner fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
    <div class="modal-dialog ">
        <div class="modal-content">
             {{ dishInfo.id }}
         </div>
     </div>
 </div>

但是,当模式窗口(点击图片后)已经开通 - 我看空块,没有ID。为什么?

But when modal window(after click on image) has been opened - i see empty block, no id. why?

推荐答案

我想通了

(如果你想继续做同样的方式)

(if you want to keep doing it the same way)

Plunker: http://plnkr.co/edit/VCewVczN4t54PQ6bf1Rh?p=preVIEW
更改

Plunker: http://plnkr.co/edit/VCewVczN4t54PQ6bf1Rh?p=preview Change

ng-click="dishInfo = dish"

ng-click="$parent.dishInfo = dish"

原来你必须明确提及的范围在$父对象的形式,因为NG-点击创建它自己的范围。

Turns out you have to explicitly refer to the scope in the form of the $parent object because ng-click creates it's own scope.

Plunker: http://plnkr.co/edit/8VOFkOP1r1RfILZ7gKfc?p=preVIEW

修改

ng-click="dishInfo = dish"

ng-click="setDish(dish)"

和在你的js文件名为setDish的方法,做类似

And have a method in your js file called setDish that does something like

$scope.setDish = function(dish) {
    $scope.dishInfo = dish;
}

这篇关于坐落在指令$范围VAR在NG-点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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