如何使用 Angular 按下按钮来更改 View 中的某些变量?
例如 View :
<div id="someDiv" ng-app="myApp" ng-controller="myCtrl">
<p ng-bind="firstname"></p>
<button type="button" ng-click="changeValue('Joe')"></button>
</div>
脚本:
var app = angular.module('myApp', []);
app.controller("myCtrl", function($scope){
$scope.firstname = "Johny";
$scope.changeValue= function(param) {
$scope.firstname = param;
}
});
我预计当我按下按钮时,Johny 将成为 Joe(如参数中所示)。 当我点击按钮时什么也没有发生
<小时/>@编辑 好吧我知道问题出在哪里了。这在我的主分区上工作得很好。但是它不适用于我的第二个 div:
我通过单击其他按钮运行下面的脚本,并且该脚本更改了我的 div
function show(div_id){ //div_id is `someDiv`
document.getElementById('main_place').innerHTML = document.getElementById(div_id).innerHTML;
}
但是为什么 Angular 脚本在这个 div 上不起作用?
最佳答案
请检查那个。
HTML:
<div id="someDiv" ng-app="myApp" ng-controller="myCtrl">
<p ng-bind="firstname"></p>
<button type="button" ng-click="changeValue('Joe')"></button>
</div>
JS:
var myApp = angular.module('myApp',[]);
myApp.controller("myCtrl", function($scope){
$scope.firstname = "Johny";
$scope.changeValue= function(param) {
$scope.firstname = param;
}
});
我针对您的问题创建了工作示例:
关于javascript - 当我按下 angularJS 中的按钮时如何更改变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37390858/