veldise / js-examples

Javascript/NodeJS/AnlaurJS/Etc Code Examples
MIT License
0 stars 1 forks source link

custom directive 설정할 때, 헷갈린 점 #3

Open JoonseokRyu opened 10 years ago

JoonseokRyu commented 10 years ago

자신이 만들려는 이름을 angular.module('exampleDirective', [])에 넣고,

shoppingModule.directive('myDirective', function () {
    return {
        restrict: 'A',
        templateUrl: './shopping.html',
        controller: cc // <- controller  설정
    };
});

처럼 controller를 설정해 주는 부분이 존재한다.

<body ng-controller ="CartController">

에 controller를 설정해 주는 부분이 있다하더라도 작동하지 않는다.

veldise commented 10 years ago

이슈가 잘 이해가 되지 않는데, cc 라는 변수가 함수인가요?

directive에 설정하는 controller는 여러가지 활용법이 있겠지만 저는 어떤 기능이나 메서드를 제공하기 위함이라고 생각합니다.

예를 들면 아래와 같습니다.

shoppingModule.directive('myDirective', function () {
    return {
        restrict: 'A',
        templateUrl: './shopping.html',
        controller: ['$scope', '$http', function MyDirectiveCtrl ($scope, $http) {
            $scope.getValue = function (callback) {
                return $http.get(url).success(function (data) {
                    callback(data);
                });
            };
        }],
        link: function ($scope, iElement, iAttr, controller) {
            $scope.click = function () {
                controller.getValue(function (data) {
                    alert(data);
                });
            };
        }
    };
});
JoonseokRyu commented 10 years ago

cc 는 함수가 맞습니다. 예전에 올려두고 지금 확인했는데, 그때 무엇을 궁금해 했는지, 저도 잘 모르겠네요. 잘 질문하는 법을 좀더 생각해 봐야 겠습니다. ;;

veldise commented 10 years ago

위 코멘트 예제를 살짝 수정하였습니다.