简体   繁体   English

为什么我的角度数据绑定不会在视图中更新?

[英]Why won't my angular data-bindings update in the view?

I am working on a basic JavaScript racer program, and I'm trying to use Angular to update the score in the view. 我正在开发一个基本的JavaScript racer程序,我正在尝试使用Angular来更新视图中的分数。 I have written the basic code to move the players and have different game states. 我已经编写了基本代码来移动玩家并拥有不同的游戏状态。

You can see how I set up my code here: 您可以在此处查看我如何设置代码:

http://jsfiddle.net/ey8dk22r/1/ http://jsfiddle.net/ey8dk22r/1/

For some reason it isn't showing up in the fiddle, but on my end the angular code will show "0" for each score. 出于某种原因,它没有出现在小提琴中,但在我的结尾,角度代码将为每个分数显示“0”。 If I change $scope.score_one or two to something like "hello", the word "hello" will show up in its place. 如果我将$ scope.score_one或两个更改为“hello”之类的内容,则“hello”一词将显示在其位置。 Currently, I have {{ score_one }} and {{ score_two }} set to player1.score and player2.score. 目前,我将{{score_one}}和{{score_two}}设置为player1.score和player2.score。 Whatever the number is initially set to will be what shows up (0). 无论最初设置的数字是什么,都会出现(0)。 When a player wins and the player's score is successfully updated, the score in the view is not updated. 当玩家获胜并且玩家的分数被成功更新时,视图中的分数不会更新。

Here is part of my js file; 这是我的js文件的一部分; the Player object and the scoreController: Player对象和scoreController:

function Player(element_id, name){
  this.el = $(element_id).selector;
  this.position = 1;
  this.name = name;
  this.score = 0;
  this.check = function(){
    if ($(this.el + ' td:last-child').hasClass("active")){
      this.score += 1;
      $(".winner").html("<h2>" + this.name + " wins!</h2>")
      game = false;
      $(".reset").show();
    }
  }
  this.move = function(position){
    $(this.el + ' td').removeClass('active');
    $(this.el + ' td:eq(' + (this.position) + ')').addClass("active");
    this.check();
    this.position += 1;
  }
}

function scoreController($scope){
  $scope.score_one = player1.score;
  $scope.score_two = player2.score;
}

And here is where I do the data-binding in the html file: 这是我在html文件中进行数据绑定的地方:

<div class="container" ng-app="" ng-controller="scoreController">

    <h1>A Day at the Races</h1>

    <ol>
        <li ng-model="score_one">Mew: {{ score_one }}</li>
        <li ng-model="score_two">Celebi: {{ score_two }}</li>
    </ol>

So, I'm wondering if I have something wrong with the scope, and why the score is not correctly updating. 所以,我想知道我的范围是否有问题,以及为什么分数没有正确更新。 I do correctly have angular linked and I was trying things with input fields that was correctly updating the view, but for some reason this isn't working. 我正确地进行了角度链接,我正在尝试使用正确更新视图的输入字段,但由于某种原因,这不起作用。 I looked at $apply and tried $scope.score_one in $apply(), but I'm not sure if I did that correctly. 我查看了$ apply并在$ apply()中尝试了$ scope.score_one,但我不确定我是否正确执行了此操作。

Let me know if you have any ideas. 如果您有任何想法,请告诉我。 Thank you. 谢谢。

I think your issue has more to do with how you are setting up your code, rather than a specific syntax problem. 我认为您的问题更多地与您设置代码的方式有关,而不是特定的语法问题。 In general, jQuery and Angular don't mesh too well together. 一般来说,jQuery和Angular不能很好地融合在一起。 From the Angular docs: 来自Angular文档:

Does Angular use the jQuery library? Angular是否使用jQuery库?

Yes, Angular can use jQuery if it's present in your app when the application is being >bootstrapped. 是的,当应用程序被引导时,Angular可以使用jQuery,如果它存在于您的应用程序中。 If jQuery is not present in your script path, Angular falls back to its own >implementation of the subset of jQuery that we call jQLite. 如果你的脚本路径中没有jQuery,Angular会回退到我们称之为jQLite的jQuery子集的实现。

Angular 1.3 only supports jQuery 2.1 or above. Angular 1.3仅支持jQuery 2.1或更高版本。 jQuery 1.7 and newer might work correctly with >Angular but we don't guarantee that. jQuery 1.7和更新版本可以正常使用> Angular,但我们不能保证。

I see that you're using jQuery 1.11 with Angular 1.2.15, so you fall in that "might" work correctly category. 我看到你正在使用带有Angular 1.2.15的jQuery 1.11,所以你可以选择“可能”正常工作类别。 If I were you, I would pick one of these tools and go with it. 如果我是你,我会选择其中一种工具并继续使用它。

Finally, if you are to go with Angular, you need to first instantiate your module, then define your controllers. 最后,如果你要使用Angular,你需要首先实例化你的模块,然后定义你的控制器。 Something like this would work: 像这样的东西会起作用:

var myAppModule = angular.module('myApp', []);
myAppModule.controller('scoreController', '$scope', function($scope) {
    $scope.score_one = player1.score;
    $scope.score_two = player2.score;
}

And you would build the ways to increment the scores in a similar fashion. 你会建立以类似的方式增加分数的方法。 In short, I would keep everything in Angular, and not even think about jQuery. 简而言之,我会保留Angular中的所有内容,甚至不考虑jQuery。 Angular is great for the two way bindings that you're doing in an app like this, so I think it's the best way to go. Angular非常适合您在这样的应用程序中进行的双向绑定,所以我认为这是最好的方法。 Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM