【问题标题】:How can access directive from others in angularjs?如何在 angularjs 中访问其他人的指令?
【发布时间】:2016-07-13 12:23:07
【问题描述】:

我对来自另一个人的指令访问有疑问,代码如下

app.module('myApp', [])
.directive('vikiQues', ['$http', '$compile', function($http, $compile){
return {
    restrict : 'E',
    scope : true,
    controller : [
        function (){
        }
    ],
    link : function(scope, iElement, iAttrs){
        $http.get('getOutside/1')
            .then(function(data){
                iElement.html($compile($(data.data))(scope));
                /*scope.addThere.find('div.please-wait').remove();
                scope.questionList.push({
                    text : '',
                    options : []
                });*/
            });
    }
};
}]).directive('vikiOption', ['$http', '$compile', function ($http, $compile) {
return {
    restrict: 'E',
    scope : true,
    require : '^vikiQues',
    link: function (scope, iElement, iAttrs, vikiQuesCtrl) {
        $http.get('getInside/1')
            .then(function(data){
                var _ = $(data.data);
                scope._opt = false;
                iElement.html($compile(_)(scope));
                if ( scope.$parent.questionList[scope.$parent.totalQuestionCount-1].options.length > 2)
                    _opt = true;
                scope.now = {
                    id : scope.$parent.questionList[scope.$parent.totalQuestionCount-1].options.length,
                    char : '',
                    text : '',
                    image : '',
                    removeable : scope._opt,
                };
                ques.now.options.push(scope.now);
            });
    }
};

}]);

我每次都收到这个错误:angular.min.js:117 Error: [$compile:ctreq] http://errors.angularjs.org/1.5.7/$compile/ctreq?p0=vikiQues&p1=vikiOption

那里有什么问题?

html代码:
0">
viki-ques 和 viki-option 是来自 php 的模板。 如果我删除 iElement.html($compile($(data.data))(范围)); 代码,我不能得到错误。我想我错了。 ithink $compile 或 scope 提供此错误。 (帮助)

【问题讨论】:

  • 我从 php 获得一个问题模板并将 html 设置为 viki-ques 元素并从中获得一些选项再次php并将html设置为viki-option ...够了吗?
  • 信息不足,无法提供帮助。用问题创建演示,然后很容易找出问题。

标签: angularjs angularjs-directive angularjs-scope


【解决方案1】:

你可以试试

require : 'vikiQues',

或者你应该改变

controller : [
        function (){
        }
    ],

到:

controller : function (){},

【讨论】:

  • 试试第二个
  • 你只是在猜测?第一个对 OP 的 HTML 结构没有意义。第二个只是第一个的缩小不安全版本,所以不能改变任何东西。 OP 没有添加足够的细节来回答这个问题。
【解决方案2】:

根据directives documentation (serach for Creating Directives that Communicate),您需要像这样要求您的父指令:

.directive('vikiOption', function(){
  return {
      restrict : 'E',
      require: '^vikiQues',
      template: '<li>my items</li>',
      //templateUrl: 'url to html file with html from previous line',
      scope : true,
      link : function (scope, elem, attrs, ctrl){ console.log(ctrl); }
  };
});

字符 ^ 表示 Angular 将尝试在标记层次结构中找到所需的指令上层

你需要在你的 vikiQues 指令中有一个控制器,像这样声明它

.directive('vikiQues', function(){
  return {
      restrict : 'E',
      transclude: true, //attention here!!
      scope : true,
      template: '<ul ng-transclude>item x</ul>', //and here, now you can instert vikiOption inside
      controller : function (){ //pay attention no brackets
        this.myProp = 'foo';  
      }
  };
});

你的标记使用这个:

<viki-queues>
  <viki-option>can use ng-repet for viki-option<viki-option/>
  <viki-option>to display a list of them<viki-option/>
</viki-queues>

附: templateUrl 更方便,因为您可以将标记保存在单独的文件中,或者从 asp.net 或其他模板引擎中获取它,或者只是将它们作为常见的静态 html 文件提供服务

P.P.S 希望对你有帮助

【讨论】:

  • 所以你只是重复了 OP 已经拥有的东西:^vikiQues 和控制器都在那里。
  • 我从没见过数组中的控制器声明,所以不一样
  • 我到处都看到了这个声明,所以我使用它但没有
  • 我在您的指令中没有看到模板/模板网址!尝试简化您的条件,声明 templateUrl 并提供模板,不要使用 $compile
  • 那么我可以告诉你[ function () {} ]function () {} 基本相同。所以function () {}没用,那你的版本也没用。
猜你喜欢
  • 2017-06-18
  • 2015-06-05
  • 2017-08-15
  • 1970-01-01
  • 2015-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多