AngularJs调用$ http inline方法

时间:2015-12-10 11:00:40

标签: javascript angularjs cordova

我没有使用angularjs的经验,只是开始使用angularjs。

我有一个输入div,我想调用http.get但是在这个div焦点更改的HTML里面,我写了这个例子:

int i=0;
 while (i>254) {
                i++;
                NSString *address = [NSString stringWithFormat:@"192.168.1.%d",i];
            struct hostent *he;
            struct in_addr ipv4addr;

            inet_pton(AF_INET, [address UTF8String], &ipv4addr);
            he = gethostbyaddr(&ipv4addr, sizeof ipv4addr, AF_INET);
            if (he) {
                printf("Host name: %s\n", he->h_name);
                NSLog(@"%@",address);
                //    NSLog(@"%@",address);
            }
        }

但是在尝试运行此代码时,我收到此错误:

<input  ng-focus="$http.get('http://server/GeneralHandler.ashx?method=GetDataFromHandler&Parameters=5,633,2015-12-12,2015-12-12,0').then( function(res) {alert(res);},function(err) {alert(err);})" id='VacationTypeID' class='details-value' ng-model='VacationTypeID' name='VacationTypeID' ng-init="VacationTypeID= '88'" />`

我想在ng-focus中编写调用http请求的内联方法,我事先并不知道这个方法是什么,我将从后端检索其内容。

请帮忙吗?

1 个答案:

答案 0 :(得分:0)

但实际上您无法在控制器中使用此代码?

$scope.httpGet = function(url) {
    $http.get(url).then( 
        function(res) {
            alert(res);
        },
        function(err) {
            alert(err);
        }
    );
}

并修改HTML ...

<input ng-focus="httpGet('http://server/GeneralHandler.ashx?method=GetDataFromHandler&Parameters=5,633,2015-12-12,2015-12-12,0')" id='VacationTypeID' class='details-value' ng-model='VacationTypeID' name='VacationTypeID' ng-init="VacationTypeID= '88'" />

所以你可以允许你的后端传递正确的网址(但最终是$scope.httpGet()的其他参数)