动态修改 directive 的templateUrl 只有在controller中生效,在directive中操作不生效

<content url="{{contentUrl}}"></<content>

//在controller中调用changeContentUrl 页面模板可以更换. 但是在下方homeAction点击事件中操作url 不生效
var home = angular.module("home", [ "common" ]);

    home.controller("homeCtrl", function($scope, commonFactory) {
    
        $scope.contentUrl = "template/userInfo";
        $scope.changeContentUrl = function(contentUrl) {
             console.log(contentUrl);
            $scope.contentUrl = "adada"; 
        };
    });
    home.directive("content", function() {
        return {
            restrict : "E",
            template : "<div ng-include='getContentUrl()'></div>",
            link : function($scope, $element, $attr) {
                $scope.getContentUrl = function() {
                    return $attr.url;
                };
            }
        };
    });
home.directive("homeAction",function(commonFactory){
        return function($scope,element,attrs){
                var node =  element.find("li");
                node.on("click",function(event){
                    var url = $(event.target).attr("url");
                    $scope.contentUrl=url;
                    console.log($scope.contentUrl);
                });
            
        };
    });
阅读 3.4k
1 个回答

homeAction 有问题:

home.directive("homeAction",function(commonFactory){
    return function($scope,element,attrs){
        var node =  element.find("li");
        node.on("click",function(event){
            var url = $(event.target).attr("url");
            $scope.contentUrl=url;
            console.log($scope.contentUrl);
        });
    };
});

function(commonFactory){ 没有返回 directive 的配置信息,需要返回 JSON。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进