控制器参数应该改变文本

时间:2016-10-31 10:15:50

标签: angularjs

$ scope.man = “A”; //默认文字

    <div ng-app="myApp" ng-controller="myCtrl">
        <p>person clicked {{man}}</p>
        <button ng-click="man('b')">B</button><br>
        <button ng-click="man('c')">C</button>
    </div>
var app=angular.module("myApp",[]);
app.controller("myCtrl",function($scope){
        $scope.man="A";
    $scope.man=function(value)
    {
        $scope.man=value;
    }
});

我是angularjs的新手我想通过参数更改文本但是文本没有改变而且默认文本A也没有显示可以帮助我解决这个问题

检查此链接 https://jsfiddle.net/nikhila/31gz56tn/

1 个答案:

答案 0 :(得分:0)

您的$ scope变量和函数是相同的。像这样改变你的功能,

<强> HTML:

<div ng-app="myApp" ng-controller="myCtrl">
        <p>person clicked {{man}}</p>
        <button ng-click="change('b')">B</button><br>
        <button ng-click="change('c')">C</button>
</div>

<强>控制器:

var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
  $scope.man = "A";
  $scope.change = function(value) {
    $scope.man = value;
  }
});

<强> DEMO