如何在$ scope中更改变量的值?

时间:2015-03-19 19:31:53

标签: javascript angularjs

我想在用户点击特定网址时将$scope.complete的值更改为false。 Angular有可能吗?

我根据此变量向用户显示信用卡表单。在第一页加载时,变量是真/假,基于来自服务器端的值,该值表明是否存在信用卡。

如果存在信用卡,那么我想要一个change网址。单击时,会将$scope.complete的值更改为false,并显示信用卡表单。

这是我目前的代码:

JS

  $scope.complete = false;
  djangoAuth.profile().then(function(data){
    $scope.model = data;
    if ($scope.model.profile.stripe_id.length <= 0)
      $scope.complete = false;
    else
      $scope.complete = true;
  });

HTML:

<div ng-if="complete == false">
 <!--show form-->
</div>
<div ng-if="complete == true">
 Credit card already on file. <a href="#">Change?</a>
</div>

3 个答案:

答案 0 :(得分:0)

看到这个小提琴显示ng-click on archive()

http://jsfiddle.net/dakra/U3pVM

  <a href="" ng-click="archive()">archive</a>

  $scope.archive = function() {
    var oldTodos = $scope.todos;
    $scope.todos = [];
    angular.forEach(oldTodos, function(todo) {
      if (!todo.done) $scope.todos.push(todo);
    });
  };

这不是我的小提琴,但展示了如何使用它。

文档也在:

https://docs.angularjs.org/api/ngTouch/directive/ngClick

答案 1 :(得分:0)

您可以将函数绑定到ng-cick并让函数更改属性的值:

&#13;
&#13;
var myApp = angular.module('myApp', []);

function MyCtrl($scope) {
    $scope.complete = true;

    $scope.change = function() {
      $scope.complete = false;
    };
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
    <div ng-show="complete == false">
        form here
    </div>
    <div ng-show="complete == true">
         Credit card already on file. <a href="#" ng-click="change()">Change?</a>
    </div>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

JS

$scope.complete = false;
  djangoAuth.profile().then(function(data){
    $scope.model = data;
    // default scope
    $scope.complete = true;
    $scope.change = function(){
        $scope.complete = true;
    }
    if ($scope.model.profile.stripe_id.length <= 0)
      $scope.complete = false;
    else
      $scope.complete = true;

  });

HTML:

<div ng-if="complete == false">
 <!--show form-->
</div>
<div ng-if="complete == true">
 Credit card already on file. <a ng-click="change()" ng-href="#">Change?</a>
</div>