AngularJS:指令ng-transclude到输入值

时间:2014-05-29 19:48:57

标签: javascript angularjs angularjs-directive transclusion

我正在尝试创建一个指令,该指令将ng-transclude值添加到html模板中的输入字段值属性:

指令我创建了:

module.directive('editInput', function(){
    return {
        restrict: 'E',
        scope: {
            value: '='
        },
        transclude: true,
        template: '<p ng-show="value == false" ng-transclude></p>' +
        '<input ng-show="value == true" placeholder="" value="" ng-transclude/>'
    }
});

寻找在输入元素中将 ng-transclude 值添加到value属性的内容

模板:

<edit-input value="isEditModeActive">{{person.name}}</edit-input>

目前我得到这个html输出:

<input ng-show="value == true" placeholder="" value="" ng-transclude="" class="">
<span class="ng-binding">Name</span></input>

但我确实需要这个html输出:

<input ng-show="value == true" placeholder="" value="Name">

1 个答案:

答案 0 :(得分:1)

的script.js:

angular.module('app', [])
.controller('ctrl', function($scope) {
  $scope.person = {};
  $scope.person.name = 'Rahul';
})
.directive('editInput', function(){
    return {
        restrict: 'E',
        scope: {
            value: '=',
            editName: '@'
        },
        transclude: true,
        template: 
        '<p ng-show="value == false" ng-transclude></p>' +
        '<input ng-show="value == true" placeholder="" value="{{editName}}" />'
    }
});

的index.html:

<!doctype html>
<html lang="en" ng-app="app">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body ng-controller="ctrl">
    <script src= "angular.js"></script>

    <script src= "script.js"></script>

    <edit-input value="true" edit-name="{{person.name}}">{{person.name}}</edit-input>
    {{person.name}}
</body>
</html>