如何将模型数据传递到发布请求?

问题描述:

如何获取ng-model的值并将其放入控制器发布请求中?

How do I get the value of an ng-model and place it into a controller post request?

    (function(angular) {
    angular.module('urlShortener', ['ngAnimate'])
        .controller('shortenerController', function($scope, $http){
            $scope.customMode = false;
            $scope.passData = function passData(){
                $http.post('/add', {url: raw_url})
                    .success(function(data, status, headers, config) {
                        // this callback will be called asynchronously
                        // when the response is available
                    })
                    .error(function(data, status, headers, config) {
                        // called asynchronously if an error occurs
                        // or server returns response with an error status.
                    });
            }
        })


})(window.angular);

我正在使用的模型:

<input ng-model="raw_url" type="text" placeholder="Paste your url here and we will shrink it!" class="form-control"/>

从范围中读取它: $ scope.raw_url

文档:

ngModel指令使用NgModelController将输入,选择,文本区域(或自定义窗体控件)绑定到作用域上的属性,该属性由该指令创建和公开.

The ngModel directive binds an input,select, textarea (or custom form control) to a property on the scope using NgModelController, which is created and exposed by this directive.