无法将angularjs控制器变量绑定到html页面

时间:2016-02-23 21:26:43

标签: javascript jquery angularjs ajax data-binding

我有一个简单的查询,我无法理解为什么它对我不起作用。

我有一个ajax调用,我从后端获取数据。这里是。

$.ajax({
    type: "GET",
    url: serviceURL,
}).then(function (response) {
    $scope.$apply(function () {
        $scope.username = response.FirstName;
    });
    console.log($scope.username);
});

我可以在这里获得控制台输出。意味着ajax工作正常。现在我需要在前端显示它。这是代码。

<li ng-model="username">[USERNAME should comeHere]</li>

我使用ng-model将控制器端数据绑定到前端。所以用户名应显示在我上面提到的[]括号之间。但是,这种情况并没有发生。

有人可以告诉我,我做错了什么。 ?我也使用了$ apply,因为我在某处读到了这将迫使它进行更新。

1 个答案:

答案 0 :(得分:1)

应该是:

<li>{{username}}</li>

这可能有助于阐明这一点:What's the difference between ng-model and ng-bind