简体   繁体   English

如何以angularjs方式执行以下jquery代码

[英]How to do the following jquery code in angularjs way

I am new to angularjs and want to do the following in angularjs way 我是angularjs的新手,并希望以angularjs的方式执行以下操作

Controller 调节器

$scope.Filter = function($event, active, id) {
    html = "";
         if(active){
                    $http({method: 'GET', url: "someUrl.json"}).
                    success(function(data, status) {

                    html+="<p>Hey i am inside if block</p>";                    
                    });
    $("#results-display").html(html);

               }

  else{
         $http({method: 'GET', url: "someotherUrl.json"}).
                    success(function(data, status) {

                    html+="<p>Hey i am inside else block</p>";                  
});
 $("#results-display").html(html);
    }
}

Basically i have used angularjs controller, but i am doing the jquery way inside the controller by appending the json data to html . 基本上我已经使用了angularjs控制器,但是我通过将json数据附加到html来在控制器内执行jquery方式。 How do i display returned json data in angularjs way? 如何以angularjs方式显示返回的json数据?

Any HTML manipulation should be omitted from controller code. 控制器代码中应省略任何HTML操作 If it has to be done in code, then use an Angular Directive . 如果必须用代码完成,则使用Angular Directive

But in your case directive is not required. 但是在您的情况下,不需要指令。 To angularize your example you should merely set a scope property (I've named it isActive ) and rather provide correct HTML display in your markup because scope model is the communication between your Javascript controller code and your HTML view. 为了使您的示例更具角度,您应该只设置一个scope属性(我将其命名为isActive ),并在标记中提供正确的HTML显示,因为scope模型是Javascript控制器代码和HTML视图之间的通信。

Javascript 使用Javascript

$scope.Filter = function($event, active, id) {
    if(active) {
        $http({
            method: 'GET',
            url: "someUrl.json"
        })
        .success(function(data, status) {
            // set $scope property
            $scope.isActive = true;
        });
    }
    else {
        $http({
            method: 'GET',
            url: "someotherUrl.json"
        })
        .success(function(data, status) {
            $scope.isActive = false;
        });
    }
};

This code can easily be made shorter and still do the same thing. 可以轻松地将此代码缩短,并且仍然可以执行相同的操作。

$scope.Filter = function($event, active, id) {
    $http({
        method: "GET",
        url: active ? "someUrl.json" : "someotherUrl.json"
    })
    .success(angular.bind(active, function(data, status) {
        $scope.isActive = this;
    }));
};

HTML HTML

<div id="results-display" ng-switch="isActive">
    <p ng-switch-when="true">Hey i am inside if block</p>
    <p ng-switch-when="false">Hey i am inside else block</p>
</div>

If you don't use this div anywhere in the code, you can omit its ID attribute altogether because angular attributes will act accordingly. 如果您不在代码中的任何位置使用此div,则可以完全省略其div属性,因为角度属性将相应地起作用。

More complex manipulation 更复杂的操作

In case this example is a simplified version of a more complex actual code (in case it's not just about being active or not) you can also set your text value in your controller and then bind to it in HTML. 如果此示例是更复杂的实际代码的简化版本(如果不只是处于活动状态,则还可以),您还可以在控制器中设置文本值,然后以HTML绑定到它。 Nothing's wrong in doing this as long as values are strictly primitives and there is no HTML involved. 只要值是严格的原语并且不涉及HTML,这样做就没错。 Any other scope properties are also just primitives or objects of objects/primitives. 任何其他范围属性也只是图元或对象/图元的对象。 They're all just data . 它们全都是数据

...
$scope.activityText = "Hey i am inside if block";
...

And then in HTML simply bind to this scope property 然后在HTML中只需绑定到此scope属性

<div id="results-display">
    <p ng-bind="activityText"></p>
</div>

This means that whenever you change $scope.activityText value (be it in your .Filter function or anywhere else) it will reflect in your HTML accordingly. 这意味着无论何时更改$scope.activityText值(在您的.Filter函数或其他任何位置),它都将相应地反映在HTML中。

You could also use a different notation using {{}} but I prefer the ng-bind attribute as it doesn't require you to also put ng-cloak on the element to prevent unusual display before Angular kicks in. 您也可以使用{{}}使用不同的符号,但是我更喜欢ng-bind属性,因为它不需要您也将ng-cloak放在元素上以防止在Angular启动之前出现异常显示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM