How do I get an input value into Angular's $scope?

前端 未结 2 1596
无人及你
无人及你 2021-01-12 00:47

I\'m new to Angular, and I am trying to do something really basic. Here is a part of a view (all angular files are added elsewhere) :

 
2条回答
  •  逝去的感伤
    2021-01-12 01:16

    The value will automatically be added to the $scope, but you have to type something into the input element.

    That said, you need to trigger something to get this value. For example, where you have your comment // get the scope value here -- this is triggered as soon as the controller is initialized and never called again; so, it's going to log undefined at that time. However, if you set it to a button click, or something, you will see it's available:

    And your controller:

    module.controller('ctrl',['$scope', function ($scope) {
        $scope.logId = function() {
            console.log($scope.id);
        }
    }]);
    

    Now type something into the input and click the button.

提交回复
热议问题