angular.js - 动态修改 directive 的templateUrl 只有在controller中生效,在directive中操作不生效
迷茫
迷茫 2017-05-15 17:06:15
0
1
495

<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 : "<p ng-include='getContentUrl()'></p>",
            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);
                });
            
        };
    });
迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(1)
巴扎黑

homeAction has a problem:

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){ does not return directive configuration information and needs to return JSON.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template