如何显示范围函数(在main函数中定义)到HTML页面的值?

时间:2017-08-02 12:50:18

标签: javascript html angularjs

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="FlowChartController">
 {{myText1}}

<script>
var app = angular.module('myApp', []);
app.controller('FlowChartController', function($scope) {
  $scope.click=function(){  $scope.myText1 = "myText1";}
});
</script>
</div>
</body>
</html>

这里我无法访问myText1变量,因为它是在click函数中定义的。如果我将相同的myText1变量放在click函数之外,我就可以访问它了。

3 个答案:

答案 0 :(得分:0)

首先,你需要调用点击功能:

         $scope.click = function (id,name) {
    $scope.myText1=name;
    alert(this.myText1);
            }; 
$scope.click(1,samplename);

它会自动生效!

答案 1 :(得分:0)

你需要在Html中调用函数:

<button ng-Clike="click(1,"feras")" />

并且您可以在Html中查看名称:

<p> {{myText1}} </p>

或者您可以编辑 JsFile 至:

$scope.click = function (id,name) {
$scope.myText1=name;
alert(this.myText1);
};

$scope.click(1,"feras");

答案 2 :(得分:0)

尝试以下代码

&#13;
&#13;
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="FlowChartController">
 {{text}}

<script>
var app = angular.module('myApp', []);
app.controller('FlowChartController', function($scope) {
    $scope.text = "myText1";
});
</script>
</div>
</body>
</html>
&#13;
&#13;
&#13;