在控制器中编译带有范围的templateFile

时间:2014-09-30 14:10:39

标签: javascript angularjs

我正在尝试为某种类型的列表构建一个函数,该函数会将该项目放在您单击的范围内,并以模态呈现它(例如单击某个类别中的文章,然后获取模态中的完整文章。

以下代码位于页面控制器中,而不是指令。我稍后会喜欢将此代码移到我的Modal服务上,以便更轻松地构建模态模板。

我可以从$ templateCache中检索模板,并在模态中呈现它:

app.controller('FlowController', function($log, $scope, $compile, $templateCache, Modal) {
  $scope.modal = function(){
    var template = $templateCache.get('/frontend/flow/partials/classifiedModal.html');
    Modal.dialog(template);
  };
});

现在,我想将该模板与范围结合起来:

app.controller('FlowController', function($log, $scope, $compile, $templateCache, Modal) {
  $scope.modal = function(index){
    var template = $templateCache.get('/frontend/flow/partials/classifiedModal.html');
    var compiled = $compile(template)($scope.classifieds[index]);
    Modal.large(compiled);
  };
});

这不断给我一个错误Undefined is not a function

TypeError: undefined is not a function
    at textInterpolateLinkFn (http://localhost:3001/angular/angular.js:6975:23)
    at nodeLinkFn (http://localhost:3001/angular/angular.js:6711:13)
    at compositeLinkFn (http://localhost:3001/angular/angular.js:6105:13)
    at compositeLinkFn (http://localhost:3001/angular/angular.js:6108:13)
    at publicLinkFn (http://localhost:3001/angular/angular.js:6001:30)
    at Scope.$scope.modal (http://localhost:3001/flow/flow.js:54:47)
    at Parser.functionCall (http://localhost:3001/angular/angular.js:10846:21)
    at ngEventDirectives.(anonymous function).compile.element.on.callback (http://localhost:3001/angular/angular.js:19133:17)
    at Scope.$get.Scope.$eval (http://localhost:3001/angular/angular.js:12701:28)
    at Scope.$get.Scope.$apply (http://localhost:3001/angular/angular.js:12799:23) {{title}}

0 个答案:

没有答案