search

Home  >  Q&A  >  body text

angular.js - When to use directive, controller, service in angular

var $ele = $('#container');

$ele.on('click', function (e){
    var $t = $(e.target);

    if($t.is('.file')){
        openDoc($t);
    }

    if($t.is('.nav a')){
        changeViewTo($t.attr('index'));
    }
})

function openDoc(){
    $ele......
}

function changeViewTo(){
    $ele......
}

How is the above code distributed in directive or controller or service?
openDoc and changeViewTo both operate on $ele and its child elements

Requirements:
1. The openDoc and changeViewTo methods need to be exposed to the outside world.

曾经蜡笔没有小新曾经蜡笔没有小新2816 days ago671

reply all(1)I'll reply

  • 高洛峰

    高洛峰2017-05-15 16:54:58

    When it is necessary to expand the original html标签的功能的时候用directive
    It is used when you need to transfer data between different controllers, or when you want to retain a global variable, because it has only one instance globallyservice The controller should be simple and does not need to place too much logic. It is responsible for the connection between the view and the model

    reply
    0
  • Cancelreply