简体   繁体   English

我的控制器中的作用域变量无法正常工作ng-include的src

[英]scope variable in my controller not working src of ng-include

I'm trying to load my ng-include dynamically by putting a $scope variable in the 'src' tag. 我试图通过将$ scope变量放在'src'标签中来动态加载ng-include。 Here's my code: 这是我的代码:

In controller: 在控制器中:

app.controller('myController', function($scope) {
    $scope.requests = {
        "0": { url: '/tpl-first-request.html' },
        "1": { url: '/tpl-second-request.html' }
    };

    $scope.getRequest = getRequest;

    function getRequest(request) {
        $scope.request = $scope.requests[request].url;
    }
});

In html: 在html中:

<body ng-controller="myController">
    <button ng-click="getRequest(0)">Click first one</button>
    <button ng-click="getRequest(1)">Click second one</button>
    <p ng-if="request">Request = {{request}}</p>
    <div ng-include src="request"></div>
</body>

The ng-include is not working, but the printout of "Request = {{request}}" is working fine.. You can test it in this Plunker ng-include无效,但是“ Request = {{request}}”的打印输出正常。.您可以在此Plunker中对其进行测试

Remove the slash from request path. request路径中删除斜杠。

  $scope.requests = {
    "0": { url: 'tpl-first-request.html' },
    "1": { url: 'tpl-second-request.html' }
  };

This do the job, at least in my PLNKR . 这样做,至少在我的PLNKR中

It's a plunker problem, you're using absolute URLs for your templates instead of relatives ones: 这是一个很麻烦的问题,您使用的是模板的绝对URL而不是亲戚的URL:

  $scope.requests = {
    "0": { url: 'tpl-first-request.html' },
    "1": { url: 'tpl-second-request.html' }
  };

working plunker : http://plnkr.co/edit/iTZJ64YyyxqbQyC1bOKS?p=preview 正在工作的朋克: http ://plnkr.co/edit/iTZJ64YyyxqbQyC1bOKS?p=preview

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

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