我的控制器中的范围变量不适用于ng-include的src

时间:2015-07-17 09:02:20

标签: javascript angularjs angularjs-ng-include

我试图通过在' src'中添加$ scope变量来动态加载我的ng-include。标签。这是我的代码:

在控制器中:

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;
    }
});

在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>

ng-include不起作用,但打印输出&#34; Request = {{request}}&#34;工作正常..您可以在此Plunker

中测试它

2 个答案:

答案 0 :(得分:1)

request路径中删除斜杠。

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

这样做,至少在我的PLNKR

答案 1 :(得分:0)

这是一个问题,您使用模板的绝对网址而不是亲戚网址:

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

work plunker:http://plnkr.co/edit/iTZJ64YyyxqbQyC1bOKS?p=preview