1

好的,我正在尝试创建一个网络链接指令。该链接需要一个 URL 和一个文本值。如果文本值没有值 - 文本值将等于 url。例如:

<a href="http://google.com">http://google.com</a>

链接具有文本值的示例

<a href="http://google.com">Search engine</a>

下面的代码中有两个输入标签,分别记录了 url 值和 text 值。如果文本值为空白,则文本值将等于 url 值。这是我的指令的代码:

Website1.directive('inputlink', function () {
    return {
        restrict: 'E',
        replace: false,
        scope: {
            urlvalue: '=',
            textvalue: '='
        },
        template:   '<div>' +
                        '<input type="text" ng-model="urlvalue" value="{{urlvalue}}" placeholder="Enter link URL"></input>'+
                        '<input type="text" ng-model="textvalue" value="{{textvalue}}" placeholder="Enter text"></input>' +
                    '</div>'
    }
});

因此,如果 textvalue 为空,它将等于 urlvalue,否则 textvalue 将有自己的值。

4

1 回答 1

2

您将需要在函数中更改它。像这样

link: function( $scope, $element, $attributes ) {
    if(!$scope.textvalue) {
       $scope.textvalue = $scope.urlvalue;
    }
}

根据您的要求,您可能还需要$watch它。

于 2013-10-15T23:41:32.817 回答