Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular: can't set initial value to input box

Tags:

angularjs

for my angular i have the following:

this is the html

<div ng-app='mainApp' ng-controller='myControl'>
   <mydirective datas="datas"></mydirective>
</div>

js file

var mainApp=angular.module('mainApp',[])
.controller('myControl',['$scope','$http',function($scope,$http){
    $http.get('./myjson.json').success(function(fdatas){
         $scope.datas=fdatas;
    });
}])
.directive('mydirective',function(){
     return{
       restrict:'E',
       scope:{
          datas:'='
       },
       template:"<input type='text' ng-model='inputvar'/>",
       controller:function($scope,window){
         $scope.inputvar=$scope.datas[0].name;
       }
     }
});

json file

[{ name:'test'},{name:'test2'}]

for some reason when i checked the debug console, it comes undefined and initial value can't be set. any idea?

thank you

like image 756
luis Avatar asked Feb 13 '26 21:02

luis


1 Answers

The typical way to declare the model in the controller... something like:

var mainApp=angular.module('mainApp',[])
    .controller('myControl',['$scope','$http',function($scope,$http){
        $scope.inputvar = "Hello World";
        $http.get('./myjson.json').success(function(fdatas){
            $scope.datas=fdatas;
    });
}])
like image 172
andand Avatar answered Feb 15 '26 12:02

andand



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!