角度控制器中的$ scope变量未在HTML中显示

时间:2016-02-11 14:32:15

标签: javascript html angularjs

我试图在点击时将clicked_user显示到聊天窗口。但即使我在控制台中看到它,这个值也永远不会在聊天窗口中更新。

控制器代码段 -

 $(document).on('click', '.chatwin', function (e)
    {
    $scope.clicked_user = $(e.target).text();
     console.log("USER "+ $scope.clicked_user);
                $('#chat_window_1').show();
            });

HTML代码

 <div class="popup-head">
  <div class="popup-head-left pull-left"   ><img src="assets/images/AshaLogo.jpg" alt="User Image" >{{clicked_user}}</div>
<div class="popup-head-right pull-right"  style="text-align: right;"><div id="circle_green"></div></div>
</div>

另一个观察结果是,如果我在函数之外初始化这个值,例如如下所示它可以工作。你能让我知道我在这里做错了什么。

控制器代码

$scope.clicked_user =“DUMMY”

 $(document).on('click', '.chatwin', function (e) {
                //$(this).parent().parent().parent().parent().remove();
                $scope.clicked_user = $(e.target).text();
                console.log("USER "+ $scope.clicked_user);
                $('#chat_window_1').show();
            });

2 个答案:

答案 0 :(得分:2)

试试这样:

$scope.$apply(function () {
    $scope.clicked_user = $(e.target).text();
});

$scope.$apply调用digest并通知角度已发生变化。

答案 1 :(得分:0)

添加$ scope。$ apply();如下所示做了诀窍。

private void ListView_DragOver(object sender, System.Windows.DragEventArgs e)
{
    var lv = sender as ListView;
    ScrollViewer sv = FindVisualChild<ScrollViewer>(lv);

    double tolerance = 10;
    double verticalPos = e.GetPosition(lv).Y;
    double offset = 3;

    if (verticalPos < tolerance)
    {
        sv.ScrollToVerticalOffset(sv.VerticalOffset - offset);
    }
    else if (verticalPos > lv.ActualHeight - tolerance)
    {
        sv.ScrollToVerticalOffset(sv.VerticalOffset + offset);
    }
}

public static childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject
{
    // Search immediate children first (breadth-first)
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
    {
        DependencyObject child = VisualTreeHelper.GetChild(obj, i);

        if (child != null && child is childItem)
            return (childItem)child;

        else
        {
            childItem childOfChild = FindVisualChild<childItem>(child);

            if (childOfChild != null)
                return childOfChild;
        }
    }

    return null;
}