Students.html
<div class="row">
<student info="ui.Ram"></student>
</div>
Student.directive Template
<h1>{{ui.name}}</h1>
路线配置
app.config(urlRouter);
function urlRouter($routeProvider, $locationProvider) {
$routeProvider
.when('/students', {
templateUrl: 'app/views/students.html',
controller: 'prodCtrl',
controllerAs: 'ui'
})
}
自定义指令
app.directive('student', "student");
function student() {
var directive = {};
directive.restrict = 'E';
directive.templateUrl = "Student.directive.html";
directive.scope = {
ui : "=name"
}
return directive;
});
控制器
app.controller('StudentController', StudentController);
function StudentController($scope) {
$scope.Ram= {};
$scope.Ram.name = "Mahesh";
};
当我这样做时,名字(&#34; Mahesh&#34;)会反映在用户界面中。
我想在不在控制器中注入$ scope的情况下也这样做。 这样的事情。
function StudentController() {
var vm = this;
vm.Ram= {};
vm.Ram.name = "Mahesh";
return vm;
};
但价值没有反映..
任何帮助/建议都非常感谢。
感谢
答案 0 :(得分:1)
您需要使用controller as
语法来执行此操作:
<div ng-app = "app" ng-controller = "StudentController as ctrl">
<student name = "ctrl.Ram"></student>
</div>