suchen

Heim  >  Fragen und Antworten  >  Hauptteil

angular.js - angularjs $scope value undefined

  <form class="ui form" id="indexForm" name="indexForm" onsubmit="return false;">
    <span id="QuacorGradingValue" ng-model="fen" name="fen">6</span>
</form>

在controller里面为啥获取不到

    var updateData = {};
    alert("$scope.rates==="+$scope.fen);//undefined

//$scope是有值的

怪我咯怪我咯2744 Tage vor581

Antworte allen(3)Ich werde antworten

  • 伊谢尔伦

    伊谢尔伦2017-05-15 16:52:18

    <body ng-controller="controller">
    

    Antwort
    0
  • 大家讲道理

    大家讲道理2017-05-15 16:52:18

    data-ng-init='fen=6'
    

    controller:

    $scope.$watch("fen", function(){
        console.log($scope.fen);
    });
    

    http://stackoverflow.com/questions/19981627/setting-scope-property-with-ng-init-doesnt-work

    Antwort
    0
  • 天蓬老师

    天蓬老师2017-05-15 16:52:18

    假设你的controller是这样的:

    module.controller('DemoController', ['$scope', function($scope) {
        $scope.fen = '';   //为其指定一个初始值,这样在html里引用时才不会因为parent scope里没有找到`fen`变量而重新创建一个
    }]);
    

    Antwort
    0
  • StornierenAntwort