简体   繁体   English

angular.js如何逐步更改变量值

[英]angular.js how to alter variable value step by step

Here is my html code 这是我的HTML代码

<div ng-app='app'>
<div ng-controller="MyController" ng-init="myVar=7">
    {{myVar}}
    <span ng-init="myVar=myVar+5">{{myVar}},</span>
    <span ng-init="myVar=myVar+15">{{myVar}},</span>
    <span ng-init="myVar=myVar+37">{{myVar}},</span>
</div>   

and script 和脚本

var app = angular.module('app',[]);
app.controller('MyController', function() {});

The output I'm getting is 64,64,64,64 我得到的输出是64,64,64,64

but I want output as 7,12,27,64 但我想输出为7,12,27,64

I'm trying to find things like ng-repeat but I cant kept these in an array 我正在尝试查找ng-repeat之类的东西,但无法将它们保存在数组中

In every ng-init you're altering the value of myVar that is data bound to all other instances; 在每个ng-init您都在更改myVar的值,该值是绑定到所有其他实例的数据。 and that's why they all show the same. 这就是为什么它们都显示相同的原因。 So rather do: 所以宁愿:

<div ng-app='app'>
<div ng-controller="MyController" ng-init="myVar=7">
    {{myVar}}
    <span>{{myVar+5}},</span>
    <span>{{myVar+15}},</span>
    <span>{{myVar+37}},</span>
</div>   

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

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