Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/454.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在angularJS中按下按钮时如何更改变量?_Javascript_Angularjs - Fatal编程技术网

Javascript 在angularJS中按下按钮时如何更改变量?

Javascript 在angularJS中按下按钮时如何更改变量?,javascript,angularjs,Javascript,Angularjs,如何使用角度键在视图中更改某些变量 例如,视图: <div id="someDiv" ng-app="myApp" ng-controller="myCtrl"> <p ng-bind="firstname"></p> <button type="button" ng-click="changeValue('Joe')"></button> </div> 我希望当我按下按钮时,Johny将成为Joe(如参数中所示)

如何使用角度键在视图中更改某些变量

例如,视图:

<div id="someDiv" ng-app="myApp" ng-controller="myCtrl">
  <p ng-bind="firstname"></p>
  <button type="button" ng-click="changeValue('Joe')"></button>
</div>
我希望当我按下按钮时,Johny将成为Joe(如参数中所示)。 当我点击按钮时,什么也没发生


@编辑 好的,我知道问题出在哪里了。这在我的主要部门很好。但是在我的第二部门不起作用:

我通过单击“其他”按钮运行下面的脚本,该脚本更改了我的div

 function show(div_id){ //div_id is `someDiv`
        document.getElementById('main_place').innerHTML = document.getElementById(div_id).innerHTML; 
        }
但是为什么angular脚本在这个div上不起作用呢?

添加

$scope.changeValue = function(newVal) {
   $scope.firstname = newVal;
}
在html上放置
{{firstname}

添加

$scope.changeValue = function(newVal) {
   $scope.firstname = newVal;
}

在html上放置
{{firstname}

在控制器中尝试以下操作:

$scope.changeValue = function(changeName)
{
$scope.firstname = changeName;
};
鉴于:

<p>{{firstname}}</p>
{{firstname}


在控制器中尝试以下操作:

$scope.changeValue = function(changeName)
{
$scope.firstname = changeName;
};
鉴于:

<p>{{firstname}}</p>
{{firstname}

请检查一下

HTML:

我为您的问题创建了一个工作示例:

请检查一下那个

HTML:

我为您的问题创建了一个工作示例:


以下是一个工作示例

var app = angular.module('myApp', []);
app.controller("myCtrl", function($scope){
    $scope.firstname = "Johny";
    $scope.changeValue = function(param) {  
        $scope.firstname = param;
    }
});

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>    <link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>

<body>
  <div id="someDiv" ng-app="myApp" ng-controller="myCtrl">
    <p ng-bind="firstname"></p>
    <button type="button" ng-click="changeValue('Joe')">Click</button>
  </div>
</body>

</html>
var-app=angular.module('myApp',[]);
app.controller(“myCtrl”,函数($scope){
$scope.firstname=“Johny”;
$scope.changeValue=函数(参数){
$scope.firstname=param;
}
});

点击

以下是一个工作示例

var app = angular.module('myApp', []);
app.controller("myCtrl", function($scope){
    $scope.firstname = "Johny";
    $scope.changeValue = function(param) {  
        $scope.firstname = param;
    }
});

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>    <link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>

<body>
  <div id="someDiv" ng-app="myApp" ng-controller="myCtrl">
    <p ng-bind="firstname"></p>
    <button type="button" ng-click="changeValue('Joe')">Click</button>
  </div>
</body>

</html>
var-app=angular.module('myApp',[]);
app.controller(“myCtrl”,函数($scope){
$scope.firstname=“Johny”;
$scope.changeValue=函数(参数){
$scope.firstname=param;
}
});

点击

您正在视图中调用“changeValue”,但控制器中的函数名为“hidePrefs”。你是这样做的还是打字错误?只是打字错误,已经编辑过了。你的代码工作正常。当编辑的输入错误时尝试测试它。不幸的是,它不起作用…您正在从视图中调用“changeValue”,但控制器中的函数名是“hidePrefs”。你是这样做的还是打字错误?只是打字错误,已经编辑过了。你的代码工作正常。当编辑的输入错误时尝试测试它。不幸的是,不起作用…为什么同时使用braket和ng bind?好的,仍然不起作用,而且没有
ng bind
我甚至看不到默认值:
Johny
为什么同时使用braket和ng bind?好的,仍然不起作用,此外,没有
ng bind
我甚至看不到默认值:
Johny