ng-model使用指令

时间:2015-10-11 07:34:26

标签: angularjs angularjs-directive angularjs-ng-model

我尝试从指令中的html输入标记获取值,并始终得到undefined

这是我的代码:

var app = angular.module('templateApp', []);

app.controller('Ctrl', function ($scope, $http, $sce) {

  $scope.DataList = "";

  $http.post("http://localhost/Service.svc/GetDataMysql", { valval: 0 })
       .then(function (data) {
           if (data.data) {
             $scope.Data = Enumerable.From(data)
                        .Select(function (x) { return x })
                        .ToArray();
             $scope.DataList = $scope.Data[0].Value;
           }
         });
});


app.run(function ($http, $templateCache) {
    $http.get('Template.html', {
        cache: $templateCache
     });
 });

 app.directive('myDir', ['$parse', '$http', '$compile', '$templateCache',      '$location',
    function ($parse, $http, $compile, $templateCache, $location) {
        var linker = function (scope, element, attrs, ngModel) {
       element.bind('click', function () {

           var ddd = scope.selectedvalue;
           scope.DataList = Enumerable.From(scope.DataList)
           .Where(function (x) {
               return x.LeafParentId == leafParentId
           }).ToArray()

           $compile(element.contents())(scope);
       });
   };


   return {

       templateUrl: 'Templates.html',
       transclude: true,
       restrict: "E",
       scope: {
           selectedvalue: '=ngModel'
       },

       link: linker
   };
    }]);

这是我的模板:

<section ng-controller="Ctrl">
<section class="box1">
    <input type="button" name="" id="1"
           ng-value="checkData(DataList[0].Name)" 
 ng-model="selectedvalue" />             
</section>
<section class="box1">
    <input type="button" name="" id="2"
           ng-value="checkData(DataList[1].Name)" 
 ng-model="selectedvalue" />              
</section>
<section class="box1">
    <input type="button" name="" id="3"
           ng-value="checkData(DataList[2].Name)" 
 ng-model="selectedvalue" />              
</section>

这是我的html:

  <section data-ng-app="templateApp">
        <div data-ng-controller="Ctrl">

             <ng-dir ></ng-dir>
            </div>


    </section>

该指令工作正常,但当我点击其中一个输入标签时,ng-model始终返回undefined

var ddd = scope.selectedvalue;

如何从指令输入标记中获取值?

0 个答案:

没有答案
相关问题